Created
August 11, 2011 17:15
-
-
Save davidbody/1140220 to your computer and use it in GitHub Desktop.
Calculate Pi in Clojure
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
(defn calculate-pi | |
"Calculates Pi using the approximation 4 * (1 - 1/3 + 1/5 - 1/7 + …)" | |
[iterations] | |
(* 4.0 (reduce + (map eval (partition 2 (interleave (cycle '(+ -)) (map #(/ 1.0 %) (take iterations (filter odd? (iterate inc 1)))))))))) | |
(println "calculated pi =" (calculate-pi 100000)) | |
(println "Math.PI =" (. Math PI)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment