packages feed

egison-3.7.8: sample/math/number/pi.egi

;;;;;
;;;;; Calucualate Pi
;;;;;

;(define $calculate-pi
;  (lambda [$n]
;    (foldr (lambda [$x $y] (+ x (/ 1 y))) 1 (take n {3 7 15 1 292 @(repeat1 1)}))))

;(define $odds (map (compose (* $ 2) (- $ 1)) nats))

;(define $calculate-pi
;  (lambda [$n]
;    (+ 3 (foldr (lambda [$x $y] (/ x (+ 6 y))) 1 (take n (map (power $ 2) odds))))))

(define $calculate-pi
  (lambda [$n]
    (/ 4 (foldr (lambda [$x $y] (+ (- (* 2 x) 1) (/ (power x 2) y))) 1 (take n nats)))))

(test (calculate-pi 1))
(test (calculate-pi 2))
(test (calculate-pi 3))
(test (calculate-pi 4))
(test (calculate-pi 5))
(test (calculate-pi 6))
(test (calculate-pi 7))
(test (calculate-pi 8))
(test (calculate-pi 9))
(test (calculate-pi 10))
(test (rtof (calculate-pi 100)))
(test (rtof (calculate-pi 1000)))
(test (rtof (calculate-pi 2000)))
(test pi)