Skip to content

Instantly share code, notes, and snippets.

@singhgurjeet243
Last active September 9, 2015 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singhgurjeet243/208a345c17e8a35fc0be to your computer and use it in GitHub Desktop.
Save singhgurjeet243/208a345c17e8a35fc0be to your computer and use it in GitHub Desktop.
(ns pi.core
(:gen-class))
(set! *unchecked-math* :warn-on-boxed)
(defn gen-point [] [(- (* 2.0 (rand)) 1.0) (- (* 2.0 (rand)) 1.0)])
(defn test-point [[^double x ^double y]] (< (+ (* x x) (* y y)) 1.0))
(defn gen-and-test-point []
(let [x (- (* 2.0 (rand)) 1.0)
y (- (* 2.0 (rand)) 1.0)]
(if (< (+ (* x x) (* y y)) 1.0) 1 0)))
(defn sample1
[n]
(transduce (map (fn [i] (if (test-point (gen-point)) 1 0))) + (range n)))
(defn sample2
[n]
(transduce (map (fn [i] (gen-and-test-point))) + (range n)))
(defn estimate-pi
[n, sample] (* 4.0 (/ (sample n) n)))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(time (estimate-pi 10000000 sample1))
(time (estimate-pi 10000000 sample2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment