From 9b2bc3a3dc182ad0e711bee2a0af628ecc85f990 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Fri, 2 Mar 2012 19:22:10 -0500 Subject: [PATCH] test case that uses ramanujan's series for approximating pi --- tests/more-tests/ramanujan-pi.expected | 1 + tests/more-tests/ramanujan-pi.rkt | 35 ++++++++++++++++++++++++++ tests/run-more-tests.rkt | 1 + 3 files changed, 37 insertions(+) create mode 100644 tests/more-tests/ramanujan-pi.expected create mode 100644 tests/more-tests/ramanujan-pi.rkt diff --git a/tests/more-tests/ramanujan-pi.expected b/tests/more-tests/ramanujan-pi.expected new file mode 100644 index 0000000..60fd1e9 --- /dev/null +++ b/tests/more-tests/ramanujan-pi.expected @@ -0,0 +1 @@ +3.1415926535897927 diff --git a/tests/more-tests/ramanujan-pi.rkt b/tests/more-tests/ramanujan-pi.rkt new file mode 100644 index 0000000..38262c0 --- /dev/null +++ b/tests/more-tests/ramanujan-pi.rkt @@ -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) \ No newline at end of file diff --git a/tests/run-more-tests.rkt b/tests/run-more-tests.rkt index b955b44..7cee348 100644 --- a/tests/run-more-tests.rkt +++ b/tests/run-more-tests.rkt @@ -52,3 +52,4 @@ (test "more-tests/scheme-whalesong.rkt") (test "more-tests/nestedloop.rkt") (test "more-tests/nucleic2.rkt") +(test "more-tests/ramanujan-pi.rkt") \ No newline at end of file