Debian Codegraph @

benchmarks.larceny/src/pi.scm

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