Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
28
Task/Average-loop-length/Elixir/average-loop-length.elixir
Normal file
28
Task/Average-loop-length/Elixir/average-loop-length.elixir
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
defmodule RC do
|
||||
def factorial(0), do: 1
|
||||
def factorial(n), do: Enum.reduce(1..n, 1, &(&1 * &2))
|
||||
|
||||
def loop_length(n), do: loop_length(n, HashSet.new)
|
||||
|
||||
defp loop_length(n, set) do
|
||||
r = :random.uniform(n)
|
||||
if Set.member?(set, r), do: Set.size(set),
|
||||
else: loop_length(n, Set.put(set, r))
|
||||
end
|
||||
|
||||
def task(runs) do
|
||||
IO.puts " N average analytical (error) "
|
||||
IO.puts "=== ========= ========== ========="
|
||||
Enum.each(1..20, fn n ->
|
||||
sum_of_runs = Enum.reduce(1..runs, 0, fn _,sum -> sum + loop_length(n) end)
|
||||
avg = sum_of_runs / runs
|
||||
analytical = Enum.reduce(1..n, 0, fn i,sum ->
|
||||
sum + (factorial(n) / :math.pow(n, i) / factorial(n-i))
|
||||
end)
|
||||
:io.format "~3w ~9.4f ~9.4f (~6.2f%)~n", [n, avg, analytical, abs(avg/analytical - 1)*100]
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
runs = 100_000
|
||||
RC.task(runs)
|
||||
|
|
@ -1,38 +1,37 @@
|
|||
/*REXX pgm computes avg loop length mapping a random field 1..N to 1..N*/
|
||||
parse arg runs tests seed .
|
||||
if runs ==',' | runs =='' then runs = 40 /*num of runs. */
|
||||
if tests ==',' | tests =='' then tests = 1000000 /*num of trials.*/
|
||||
if seed\==',' & seed\=='' then call random ,,seed /*repeatability?*/
|
||||
numeric digits 100000; !.=0; !.0=1 /*be able to calculate !(25000).*/
|
||||
numeric digits max(9,length(!(runs))) /*set NUMERIC digits for !(runs).*/
|
||||
say right( runs, 24) 'runs' /*display # of runs we're using*/
|
||||
say right( tests, 24) 'tests' /* " " " tests " " */
|
||||
say right( digits(), 24) 'digits' /* " " " digits " " */
|
||||
/*REXX pgm computes average loop length mapping a random field 1..N ───► 1..N */
|
||||
parse arg runs tests seed . /*obtain optional arguments from C.L. */
|
||||
if runs ==',' | runs =='' then runs = 40 /*number of runs. */
|
||||
if tests ==',' | tests =='' then tests= 1000000 /* " " trials. */
|
||||
if seed\==',' & seed\=='' then call random ,,seed /*RAND repeatability?*/
|
||||
numeric digits 100000; !.=0; !.0=1 /*be able to calculate 25,000! */
|
||||
numeric digits max(9,length(!(runs))) /*set the NUMERIC DIGITS for !(runs). */
|
||||
say right( runs, 24) 'runs' /*display number of runs we're using.*/
|
||||
say right( tests, 24) 'tests' /* " " " tests " " */
|
||||
say right( digits(), 24) 'digits' /* " " " digits " " */
|
||||
say
|
||||
say ' N average exact % error' /*headers & pad.*/
|
||||
h= ' ─── ───────── ───────── ─────────'; say h; pad=left('',3)
|
||||
|
||||
do #=1 for runs; ##=right(#,9) /*## is used for indenting output*/
|
||||
avg = fmtD(exact(#)) /*use 4 digits past decimal point*/
|
||||
exa = fmtD(exper(#)) /* " " " " " " */
|
||||
err = fmtD(abs(exa-avg)*100/avg) /* " " " " " " */
|
||||
say ## pad exa pad avg pad err /*display a line of statistics. */
|
||||
end /*#*/
|
||||
say h /*display the final header bar. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────! subroutine────────────────────────*/
|
||||
!: procedure expose !.; parse arg z; if !.z\==0 then return !.z
|
||||
!=1; do j=1 for z; !=!*j; !.j=!; end; return !
|
||||
/*──────────────────────────────────EXACT subroutine────────────────────*/
|
||||
exact: parse arg x; s=0; do j=1 for x; s=s+!(x)/!(x-j)/x**j; end; return s
|
||||
/*──────────────────────────────────EXPER subroutine────────────────────*/
|
||||
exper: parse arg n
|
||||
k=0; do tests; $.=0 /*repeat TESTS times, reset FOUND*/
|
||||
do n; r=random(1,n); if $.r then leave
|
||||
$.r=1; k=k+1 /*bump the ctr. */
|
||||
end /*n*/
|
||||
end /*tests*/
|
||||
return k/tests
|
||||
/*──────────────────────────────────FMTD subroutine─────────────────────*/
|
||||
fmtD: parse arg y,d; d=word(d 4,1); y=format(y,,d); parse var y w '.' f
|
||||
if f=0 then return w || left('',d+1); return y
|
||||
say ' N average exact % error' /*◄──title,header►───┐*/
|
||||
h= ' ─── ───────── ───────── ─────────'; pad=left('',3) /*◄──────┘*/
|
||||
say h
|
||||
do #=1 for runs; ##=right(#,9) /*## is used for indenting the output.*/
|
||||
avg=fmtD(exact(#)) /*use four digits past decimal point. */
|
||||
exa=fmtD(exper(#)) /* " " " " " " */
|
||||
err=fmtD(abs(exa-avg)*100/avg) /* " " " " " " */
|
||||
say ## pad exa pad avg pad err /*display a line of statistics to term.*/
|
||||
end /*#*/
|
||||
say h /*display the final header (some bars).*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
!: procedure expose !.; parse arg z; if !.z\==0 then return !.z
|
||||
!=1; do j=1 for z; !=!*j; !.j=!; end; /*factorial*/ return !
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
exact: parse arg x; s=0; do j=1 for x; s=s+!(x)/!(x-j)/x**j; end; return s
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
exper: parse arg n; k=0; do tests; $.=0 /*do it TESTS times.*/
|
||||
do n; r=random(1,n); if $.r then leave
|
||||
$.r=1; k=k+1 /*bump the counter. */
|
||||
end /*n*/
|
||||
end /*tests*/
|
||||
return k/tests
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
fmtD: parse arg y,d; d=word(d 4,1); y=format(y,,d); parse var y w '.' f
|
||||
if f=0 then return w || left('', d+1); return y
|
||||
|
|
|
|||
37
Task/Average-loop-length/Scala/average-loop-length.scala
Normal file
37
Task/Average-loop-length/Scala/average-loop-length.scala
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import scala.util.Random
|
||||
|
||||
object AverageLoopLength extends App {
|
||||
|
||||
val factorial: Stream[Double] = 1 #:: factorial.zip(Stream.from(1)).map(n => n._2 * factorial(n._2 - 1))
|
||||
|
||||
def expected(n: Int) = (for (i <- 1 to n) yield factorial(n) / Math.pow(n, i) / factorial(n - i)).sum
|
||||
|
||||
def trial(n: Int):Double = {
|
||||
var count = 0
|
||||
var x = 1
|
||||
var bits = 0
|
||||
|
||||
while ((bits & x) == 0) {
|
||||
count = count + 1
|
||||
bits = bits | x
|
||||
x = 1 << Random.nextInt(n)
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
def tested(n: Int, times: Int) = (for (i <- 1 to times) yield trial(n)).sum / times
|
||||
|
||||
val results = for (n <- 1 to 20;
|
||||
avg = tested(n, 1000000);
|
||||
theory = expected(n)
|
||||
) yield (n, avg, theory, (avg / theory - 1) * 100)
|
||||
|
||||
|
||||
println("n avg exp diff")
|
||||
println("------------------------------------")
|
||||
results foreach { n => {
|
||||
println(f"${n._1}%2d ${n._2}%2.6f ${n._3}%2.6f ${n._4}%2.3f%%")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue