File tree Expand file tree Collapse file tree 1 file changed +4
-1
lines changed
contents/monte_carlo_integration/code/clojure Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Original file line number Diff line number Diff line change 8
8
(map #(* % %))
9
9
(reduce +))
10
10
(* r r)))
11
+
11
12
(defn rand-point [r]
12
13
" return a random point from (0,0) inclusive to (r,r) exclusive"
13
14
(repeatedly 2 #(rand r)))
15
+
14
16
(defn monte-carlo [n r]
15
17
" take the number of random points and radius return an estimate to
16
18
pi"
22
24
(if (in-circle? (rand-point r) r)
23
25
(inc count)
24
26
count))))))
27
+
25
28
(defn -main []
26
29
(let [constant-pi Math/PI
27
30
computed-pi (monte-carlo 10000000 2 ) ; ; this may take some time on lower end machines
28
31
difference (Math/abs (- constant-pi computed-pi))
29
32
error (* 100 (/ difference constant-pi))]
30
33
(println " world's PI: " constant-pi
31
- " ,our PI: " (double computed-pi)
34
+ " ,our PI: " (double computed-pi)
32
35
" ,error: " error)))
You can’t perform that action at this time.
0 commit comments