test case that uses ramanujan's series for approximating pi
This commit is contained in:
parent
07d569a66b
commit
9b2bc3a3dc
1
tests/more-tests/ramanujan-pi.expected
Normal file
1
tests/more-tests/ramanujan-pi.expected
Normal file
|
@ -0,0 +1 @@
|
||||||
|
3.1415926535897927
|
35
tests/more-tests/ramanujan-pi.rkt
Normal file
35
tests/more-tests/ramanujan-pi.rkt
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#lang planet dyoo/whalesong
|
||||||
|
;; Srinivasa Ramanujan's infinite series for approximating pi.
|
||||||
|
|
||||||
|
(define (sum f a b)
|
||||||
|
(cond
|
||||||
|
[(= a b)
|
||||||
|
(f a)]
|
||||||
|
[else
|
||||||
|
(+ (f a)
|
||||||
|
(sum f (add1 a) b))]))
|
||||||
|
|
||||||
|
(define (fact x)
|
||||||
|
(cond
|
||||||
|
[(= x 0)
|
||||||
|
1]
|
||||||
|
[else
|
||||||
|
(* x (fact (sub1 x)))]))
|
||||||
|
|
||||||
|
|
||||||
|
(define (1/pi-approx n)
|
||||||
|
(* (/ (* 2 (sqrt 2))
|
||||||
|
9801)
|
||||||
|
(sum (lambda (k)
|
||||||
|
(/ (* (fact (* 4 k))
|
||||||
|
(+ 1103 (* 26390 k)))
|
||||||
|
(* (expt (fact k) 4)
|
||||||
|
(expt 396 (* 4 k)))))
|
||||||
|
0
|
||||||
|
n)))
|
||||||
|
|
||||||
|
(define (pi-approx n)
|
||||||
|
(/ 1 (1/pi-approx n)))
|
||||||
|
|
||||||
|
|
||||||
|
(pi-approx 10)
|
|
@ -52,3 +52,4 @@
|
||||||
(test "more-tests/scheme-whalesong.rkt")
|
(test "more-tests/scheme-whalesong.rkt")
|
||||||
(test "more-tests/nestedloop.rkt")
|
(test "more-tests/nestedloop.rkt")
|
||||||
(test "more-tests/nucleic2.rkt")
|
(test "more-tests/nucleic2.rkt")
|
||||||
|
(test "more-tests/ramanujan-pi.rkt")
|
Loading…
Reference in New Issue
Block a user