Skip to content

Instantly share code, notes, and snippets.

@yaraki
Created January 22, 2014 14:08
Show Gist options
  • Save yaraki/8559307 to your computer and use it in GitHub Desktop.
Save yaraki/8559307 to your computer and use it in GitHub Desktop.
Ramanujan's formulas of pi in Common Lisp
(defun fact (n)
(let ((r 1))
(loop
for i from 2 to n
do (setf r (* i r))
finally (return r))))
(defun calc-pi-1 (max)
(/ 1
(* (/ (* 2 (sqrt 2.0d0)) (expt 99 2))
(loop
for n from 0 to max
sum (/ (* (fact (* 4 n)) (+ 1103 (* 26390 n)))
(expt (* (expt 4 n) (expt 99 n) (fact n)) 4))))))
(defun calc-pi-2 (max)
(/ 4
(loop
for n from 0 to max
sum (/ (* (expt -1 n) (fact (* 4 n)) (+ 1123 (* 21460 n)))
(* (expt 882 (+ (* 2 n) 1)) (expt (* (expt 4 n) (fact n)) 4))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment