Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
15
Task/Monte-Carlo-methods/Elixir/monte-carlo-methods.elixir
Normal file
15
Task/Monte-Carlo-methods/Elixir/monte-carlo-methods.elixir
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
defmodule MonteCarlo do
|
||||
def pi(n) do
|
||||
:random.seed(:os.timestamp)
|
||||
count = Enum.count(1..n, fn _ ->
|
||||
x = :random.uniform
|
||||
y = :random.uniform
|
||||
:math.sqrt(x*x + y*y) <= 1
|
||||
end)
|
||||
4 * count / n
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each([1000, 10000, 100000, 1000000, 10000000], fn n ->
|
||||
:io.format "~8w samples: PI = ~f~n", [n, MonteCarlo.pi(n)]
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue