Skip to content

Instantly share code, notes, and snippets.

@davidbody
Created August 11, 2011 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidbody/1140220 to your computer and use it in GitHub Desktop.
Save davidbody/1140220 to your computer and use it in GitHub Desktop.
Calculate Pi in Clojure
(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