Almkvist-Giullera formula for pi: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Add implementation)
Line 181: Line 181:
; the nth integer term
; the nth integer term
(defun integral (n)
(defun integral (n)
(/r (*r 32 (!r (*r 6 n)) (+r (*r 532 n n) (*r 126 n) 9)) (*r 3 (expt-r (!r n) 6))))
(let* ((polynomial (+r (*r 532 n n) (*r 126 n) 9))
(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
; the exponent for 10 in the nth term of the series
Line 187: Line 191:


; the nth term of the series
; the nth term of the series
(defun a-g (n)
(defun almkvist-giullera (n)
(/r (integral n) (expt-r 10 (abs (power n)))))
(/r (integral n) (expt-r 10 (abs (power n)))))


; the sum of the first n terms
; the sum of the first n terms
(defun a-g-sigma (n)
(defun almkvist-giullera-sigma (n)
(let ((s 0))
(let ((s 0))
(loop for i from 0 to n doing (setq s (+r s (a-g i))))
(loop for i from 0 to n doing (setq s (+r s (almkvist-giullera i))))
s))
s))


; the approximation to pi after n terms
; the approximation to pi after n terms
(defun a-g-pi (n)
(defun almkvist-giullera-pi (n)
(sqrt-r (/r 1 (a-g-sigma n))))
(sqrt-r (/r 1 (almkvist-giullera-sigma n))))


(format t "~A. ~44A~4A ~A~%" "N" "Integral part of Nth term" "×10^" "=Actual value of Nth term")
(format t "~A. ~44A~4A ~A~%" "N" "Integral part of Nth term" "×10^" "=Actual value of Nth term")
Line 204: Line 208:
(format t "~&~a. ~44d ~3d " i (integral i) (power i))
(format t "~&~a. ~44d ~3d " i (integral i) (power i))
(finish-output *standard-output*)
(finish-output *standard-output*)
(print-r (a-g i) 50 nil))
(print-r (almkvist-giullera i) 50 nil))


(format t "~%~%Pi after ~a iterations: " *iterations*)
(format t "~%~%Pi after ~a iterations: " *iterations*)
(print-r (a-g-pi *iterations*) *print-prec*)</lang>
(print-r (almkvist-giullera-pi *iterations*) *print-prec*)
</lang>


{{Out}}
{{Out}}