OLD | NEW |
(Empty) | |
| 1 (define (pi num) |
| 2 (define (pi-brent-salamin-approximate base k) ; k is number of digits |
| 3 (define base^k (expt base k)) |
| 4 (define (fixed.+ x y) |
| 5 (+ x y)) |
| 6 |
| 7 (define (fixed.- x y) |
| 8 (- x y)) |
| 9 |
| 10 (define (fixed.* x y) |
| 11 (quotient (* x y) base^k)) |
| 12 |
| 13 (define (fixed.square x) |
| 14 (fixed.* x x)) |
| 15 |
| 16 (define (fixed./ x y) |
| 17 (quotient (* x base^k) y)) |
| 18 |
| 19 (define (fixed.sqrt x) |
| 20 (integer-sqrt (* x base^k))) |
| 21 |
| 22 (define (number->fixed x) |
| 23 (round (* x base^k))) |
| 24 |
| 25 (define (fixed->number x) |
| 26 (/ x base^k)) |
| 27 |
| 28 (let ((one (number->fixed 1))) |
| 29 (let loop ((a one) |
| 30 (b (fixed.sqrt (quotient one 2))) |
| 31 (t (quotient one 4)) |
| 32 (x 1)) |
| 33 (if (= a b) |
| 34 (quotient (* a a) t) |
| 35 (let ((new-a (quotient (fixed.+ a b) 2))) |
| 36 (loop new-a |
| 37 (integer-sqrt (* a b)) |
| 38 (fixed.- t (* x (fixed.square (fixed.- new-a a)))) |
| 39 (* 2 x))))))) |
| 40 |
| 41 (define (pi-brent-salamin base k) ; k is number of digits |
| 42 (let ((n (ceiling (inexact->exact (+ 2 (log k)))))) |
| 43 (quotient (pi-brent-salamin-approximate base (+ k n)) (expt base n)))) |
| 44 |
| 45 (pi-brent-salamin-approximate 10 num)) |
OLD | NEW |