| 1 |
;;; PI -- Compute PI using bignums. |
| 2 |
|
| 3 |
; See http://mathworld.wolfram.com/Pi.html for the various algorithms. |
| 4 |
|
| 5 |
(library (rnrs-benchmarks pi) |
| 6 |
(export main) |
| 7 |
(import (rnrs) (rnrs r5rs) (rnrs-benchmarks)) |
| 8 |
|
| 9 |
; Utilities. |
| 10 |
|
| 11 |
(define (width x) |
| 12 |
(let loop ((i 0) (n 1)) |
| 13 |
(if (< x n) i (loop (+ i 1) (* n 2))))) |
| 14 |
|
| 15 |
(define (root x y) |
| 16 |
(let loop ((g (expt |
| 17 |
2 |
| 18 |
(quotient (+ (width x) (- y 1)) y)))) |
| 19 |
(let ((a (expt g (- y 1)))) |
| 20 |
(let ((b (* a y))) |
| 21 |
(let ((c (* a (- y 1)))) |
| 22 |
(let ((d (quotient (+ x (* g c)) b))) |
| 23 |
(if (< d g) (loop d) g))))))) |
| 24 |
|
| 25 |
(define (square-root x) |
| 26 |
(root x 2)) |
| 27 |
|
| 28 |
(define (quartic-root x) |
| 29 |
(root x 4)) |
| 30 |
|
| 31 |
(define (square x) |
| 32 |
(* x x)) |
| 33 |
|
| 34 |
; Compute pi using the 'brent-salamin' method. |
| 35 |
|
| 36 |
(define (pi-brent-salamin nb-digits) |
| 37 |
(let ((one (expt 10 nb-digits))) |
| 38 |
(let loop ((a one) |
| 39 |
(b (square-root (quotient (square one) 2))) |
| 40 |
(t (quotient one 4)) |
| 41 |
(x 1)) |
| 42 |
(if (= a b) |
| 43 |
(quotient (square (+ a b)) (* 4 t)) |
| 44 |
(let ((new-a (quotient (+ a b) 2))) |
| 45 |
(loop new-a |
| 46 |
(square-root (* a b)) |
| 47 |
(- t |
| 48 |
(quotient |
| 49 |
(* x (square (- new-a a))) |
| 50 |
one)) |
| 51 |
(* 2 x))))))) |
| 52 |
|
| 53 |
; Compute pi using the quadratically converging 'borwein' method. |
| 54 |
|
| 55 |
(define (pi-borwein2 nb-digits) |
| 56 |
(let* ((one (expt 10 nb-digits)) |
| 57 |
(one^2 (square one)) |
| 58 |
(one^4 (square one^2)) |
| 59 |
(sqrt2 (square-root (* one^2 2))) |
| 60 |
(qurt2 (quartic-root (* one^4 2)))) |
| 61 |
(let loop ((x (quotient |
| 62 |
(* one (+ sqrt2 one)) |
| 63 |
(* 2 qurt2))) |
| 64 |
(y qurt2) |
| 65 |
(p (+ (* 2 one) sqrt2))) |
| 66 |
(let ((new-p (quotient (* p (+ x one)) |
| 67 |
(+ y one)))) |
| 68 |
(if (= x one) |
| 69 |
new-p |
| 70 |
(let ((sqrt-x (square-root (* one x)))) |
| 71 |
(loop (quotient |
| 72 |
(* one (+ x one)) |
| 73 |
(* 2 sqrt-x)) |
| 74 |
(quotient |
| 75 |
(* one (+ (* x y) one^2)) |
| 76 |
(* (+ y one) sqrt-x)) |
| 77 |
new-p))))))) |
| 78 |
|
| 79 |
; Compute pi using the quartically converging 'borwein' method. |
| 80 |
|
| 81 |
(define (pi-borwein4 nb-digits) |
| 82 |
(let* ((one (expt 10 nb-digits)) |
| 83 |
(one^2 (square one)) |
| 84 |
(one^4 (square one^2)) |
| 85 |
(sqrt2 (square-root (* one^2 2)))) |
| 86 |
(let loop ((y (- sqrt2 one)) |
| 87 |
(a (- (* 6 one) (* 4 sqrt2))) |
| 88 |
(x 8)) |
| 89 |
(if (= y 0) |
| 90 |
(quotient one^2 a) |
| 91 |
(let* ((t1 (quartic-root (- one^4 (square (square y))))) |
| 92 |
(t2 (quotient |
| 93 |
(* one (- one t1)) |
| 94 |
(+ one t1))) |
| 95 |
(t3 (quotient |
| 96 |
(square (quotient (square (+ one t2)) one)) |
| 97 |
one)) |
| 98 |
(t4 (+ one |
| 99 |
(+ t2 |
| 100 |
(quotient (square t2) one))))) |
| 101 |
(loop t2 |
| 102 |
(quotient |
| 103 |
(- (* t3 a) (* x (* t2 t4))) |
| 104 |
one) |
| 105 |
(* 4 x))))))) |
| 106 |
|
| 107 |
; Try it. |
| 108 |
|
| 109 |
(define (pies n m s) |
| 110 |
(if (< m n) |
| 111 |
'() |
| 112 |
(let ((bs (pi-brent-salamin n)) |
| 113 |
(b2 (pi-borwein2 n)) |
| 114 |
(b4 (pi-borwein4 n))) |
| 115 |
(cons (list b2 (- bs b2) (- b4 b2)) |
| 116 |
(pies (+ n s) m s))))) |
| 117 |
|
| 118 |
(define expected |
| 119 |
'((314159265358979323846264338327950288419716939937507 |
| 120 |
-54 |
| 121 |
124) |
| 122 |
(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170673 |
| 123 |
-51 |
| 124 |
-417) |
| 125 |
(3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408122 |
| 126 |
-57 |
| 127 |
-819) |
| 128 |
(314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038195 |
| 129 |
-76 |
| 130 |
332) |
| 131 |
(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019089 |
| 132 |
-83 |
| 133 |
477) |
| 134 |
(3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141268 |
| 135 |
-72 |
| 136 |
-2981) |
| 137 |
(314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536431 |
| 138 |
-70 |
| 139 |
-2065) |
| 140 |
(31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116089 |
| 141 |
-79 |
| 142 |
1687) |
| 143 |
(3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118542 |
| 144 |
-92 |
| 145 |
-2728) |
| 146 |
(314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194907 |
| 147 |
-76 |
| 148 |
-3726))) |
| 149 |
|
| 150 |
(define (main . args) |
| 151 |
(run-benchmark |
| 152 |
"pi" |
| 153 |
pi-iters |
| 154 |
(lambda (result) (equal? result expected)) |
| 155 |
(lambda (n m s) (lambda () (pies n m s))) |
| 156 |
50 |
| 157 |
500 |
| 158 |
50))) |