Skip to content

Instantly share code, notes, and snippets.

@alecbz
Created July 3, 2011 06:47
Show Gist options
  • Select an option

  • Save alecbz/1062014 to your computer and use it in GitHub Desktop.

Select an option

Save alecbz/1062014 to your computer and use it in GitHub Desktop.
compute pi via monte carlo
(defn circle-test [x y]
(not ( > ( + (* (- x 0.5) (- x 0.5)) (* (- y 0.5) (- y 0.5))) 0.25)))
(defn error [est]
(Math/abs (* (/ (- Math/PI est) Math/PI) 100)))
(defn pi [n]
(loop [hits 0 total 0]
(let [x (rand) y (rand)]
(if (< total n)
(recur (if (circle-test x y) (inc hits) hits) (inc total))
(* (/ hits total) 4.0)))))
(defn estimate-pi [shots runs]
(loop [r 0 vals ()]
(let [est (pi shots)]
(if (= r runs)
(/ (reduce + vals) (count vals))
(recur (inc r) (conj vals est))))))
(defn print-estimate [est]
(println est)
(println (str (error est) "%")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment