Skip to content

Instantly share code, notes, and snippets.

@davidbody
Forked from bmabey/pi.clj
Created August 11, 2011 18:26
Show Gist options
  • Save davidbody/1140354 to your computer and use it in GitHub Desktop.
Save davidbody/1140354 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.0 -1.0]) (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