Hello!
First of all, I don't know if this is the right place to ask this
question. If you think not, please let me know. Maybe you think the
question is too basic. I emphasize, it's not homework.
Here's Chudnovsky's algorithm in Scheme.
(define (pi (digits))
(let* ((A 13591409)
(B 545140134)
(C 640320)
(C^3/24 (quotient (expt 640320 3) 24))
(D 12))
(define (-1^n*g n g) (if (odd? n) (- g) g))
(define (split m n)
(if (= 1 (- n m))
(let* ((6n (* 6 n)) (g (* (- 6n 5) (- (+ n n) 1) (- 6n 1))))
(list g (* C^3/24 (expt n 3)) (* (-1^n*g n g) (+ (* n B) A))))
(let* ((mid (quotient (+ m n) 2))
(gpq1 (split m mid))
(gpq2 (split mid n))
(g1 (car gpq1)) (p1 (cadr gpq1)) (q1 (caddr gpq1))
(g2 (car gpq2)) (p2 (cadr gpq2)) (q2 (caddr gpq2)))
(list (* g1 g2) (* p1 p2) (+ (* q1 p2) (* q2 g1))))))
(let* ((num-terms (inexact->exact (floor (+ 2 (/ digits
14.181647462)))))
(sqrt-C (integer-sqrt (* C (expt 100 digits))))
(gpq (split 0 num-terms))
(g (car gpq)) (p (cadr gpq)) (q (caddr gpq)))
(quotient (* p C sqrt-C) (* D (+ q (* p A)))))))
|||
|
|There's a C version of the same algorithm that is about sixteen times
as long, so Scheme is expressive indeed. What I want to do is to compare
the two implementations, ||but I get stuck and don't know how to proceed!|
|First I want to write a version of the Scheme program that asks me how
many digits I want to calculate and then calculate that using
interpretation, but I don't manage to do that. Then I want to compile
the Scheme code and see how fast it runs and how much RAM it uses.
Lastly, I want to compile the C version and run it. I want to compare
the performance and RAM usage in all three cases.
|
|Could you please help me out with the Scheme part? I guess I am
supposed to use "read" somehow, but how? I am a Scheme novice.
|
|Best regards,|
|
|
|-- |
|Torbjörn Svensson Diaz
|
|
|