Skip to content

Commit

Permalink
Add blank lines in Monte Carlo integration in Clojure
Browse files Browse the repository at this point in the history
  • Loading branch information
berquist committed Oct 26, 2020
1 parent 96ad3e4 commit 72e6a31
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
(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"
Expand All @@ -22,11 +24,12 @@ pi"
(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)
",our PI: " (double computed-pi)
",error: " error)))

0 comments on commit 72e6a31

Please sign in to comment.