Last active
September 9, 2015 05:45
-
-
Save singhgurjeet243/208a345c17e8a35fc0be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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