2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -2,20 +2,18 @@ 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)
|
||||
def loop_length(n), do: loop_length(n, MapSet.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))
|
||||
r = :rand.uniform(n)
|
||||
if r in set, do: MapSet.size(set), else: loop_length(n, MapSet.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
|
||||
avg = Enum.reduce(1..runs, 0, fn _,sum -> sum + loop_length(n) end) / runs
|
||||
analytical = Enum.reduce(1..n, 0, fn i,sum ->
|
||||
sum + (factorial(n) / :math.pow(n, i) / factorial(n-i))
|
||||
end)
|
||||
|
|
@ -24,5 +22,5 @@ defmodule RC do
|
|||
end
|
||||
end
|
||||
|
||||
runs = 100_000
|
||||
runs = 1_000_000
|
||||
RC.task(runs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue