-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathmonte_carlo.clj
35 lines (31 loc) · 1011 Bytes
/
monte_carlo.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(ns monte-carlo.core)
(defn in-circle? [pv r]
"take a vector representing point and radius return true if the
point is inside the circle"
(< (->>
pv
(map #(* % %))
(reduce +))
(* r r)))
(defn rand-point [r]
"return a random point from (0,0) inclusive to (r,r) exclusive"
(repeatedly 2 #(rand r)))
(defn monte-carlo [n r]
"take the number of random points and radius return an estimate to
pi"
(*' 4 (/ n)
(loop [i n count 0]
(if (zero? i)
count
(recur (dec i)
(if (in-circle? (rand-point r) r)
(inc count)
count))))))
(defn -main []
(let [constant-pi Math/PI
computed-pi (monte-carlo 10000000 2) ;; this may take some time on lower end machines
difference (Math/abs (- constant-pi computed-pi))
error (* 100 (/ difference constant-pi))]
(println "world's PI: " constant-pi
",our PI: " (double computed-pi)
",error: " error)))