Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Last active August 29, 2015 14:14
Show Gist options
  • Save keyvanakbary/1b9eb63d5f95afd81713 to your computer and use it in GitHub Desktop.
Save keyvanakbary/1b9eb63d5f95afd81713 to your computer and use it in GitHub Desktop.
Nilakantha's series for calculating π
(defn nilakantha [iterations]
(loop [sum 3.0M
counter 0
sign +]
(if (>= counter (* 2 iterations))
sum
(recur
(sign sum
(/ 4.0M
(* (+ counter 2)
(+ counter 3)
(+ counter 4))))
(+ counter 2)
(if (= sign +) - +)))))
(println
(with-precision 10000
(nilakantha 150)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment