We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9ca59fc commit a706a76Copy full SHA for a706a76
contents/monte_carlo_integration/code/clojure/monte_carlo.clj
@@ -8,9 +8,11 @@
8
(map #(* % %))
9
(reduce +))
10
(* r r)))
11
+
12
(defn rand-point [r]
13
"return a random point from (0,0) inclusive to (r,r) exclusive"
14
(repeatedly 2 #(rand r)))
15
16
(defn monte-carlo [n r]
17
"take the number of random points and radius return an estimate to
18
pi"
@@ -22,11 +24,12 @@ pi"
22
24
(if (in-circle? (rand-point r) r)
23
25
(inc count)
26
count))))))
27
28
(defn -main []
29
(let [constant-pi Math/PI
30
computed-pi (monte-carlo 10000000 2) ;; this may take some time on lower end machines
31
difference (Math/abs (- constant-pi computed-pi))
32
error (* 100 (/ difference constant-pi))]
33
(println "world's PI: " constant-pi
- ",our PI: " (double computed-pi)
34
+ ",our PI: " (double computed-pi)
35
",error: " error)))
0 commit comments