Skip to content

Instantly share code, notes, and snippets.

@bmabey
Forked from davidbody/pi.clj
Created August 11, 2011 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bmabey/1140255 to your computer and use it in GitHub Desktop.
Save bmabey/1140255 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]
(let [odd-numbers (filter odd? (iterate inc 1))]
(* 4.0
(apply + (map / (cycle [1 -1]) (take iterations odd-numbers))))))
(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