Almkvist-Giullera formula for pi: Difference between revisions

m
(→‎{{header|Haskell}}: Add implementation)
Line 181:
; the nth integer term
(defun integral (n)
(/r (let*r 32 (!r (*r 6 n))polynomial (+r (*r 532 n n) (*r 126 n) 9)) (*r 3 (expt-r (!r n) 6))))
(numer (*r 32 (!r (*r 6 n)) polynomial))
(denom (*r 3 (expt-r (!r n) 6))))
(/r numer denom)))
 
; the exponent for 10 in the nth term of the series
Line 187 ⟶ 191:
 
; the nth term of the series
(defun aalmkvist-ggiullera (n)
(/r (integral n) (expt-r 10 (abs (power n)))))
 
; the sum of the first n terms
(defun aalmkvist-ggiullera-sigma (n)
(let ((s 0))
(loop for i from 0 to n doing (setq s (+r s (aalmkvist-ggiullera i))))
s))
 
; the approximation to pi after n terms
(defun aalmkvist-ggiullera-pi (n)
(sqrt-r (/r 1 (aalmkvist-ggiullera-sigma n))))
 
(format t "~A. ~44A~4A ~A~%" "N" "Integral part of Nth term" "×10^" "=Actual value of Nth term")
Line 204 ⟶ 208:
(format t "~&~a. ~44d ~3d " i (integral i) (power i))
(finish-output *standard-output*)
(print-r (aalmkvist-ggiullera i) 50 nil))
 
(format t "~%~%Pi after ~a iterations: " *iterations*)
(print-r (aalmkvist-ggiullera-pi *iterations*) *print-prec*)</lang>
</lang>
 
{{Out}}
1,479

edits