Digits Of E
June 19, 2012
We begin with the algorithm by Rabinowitz and Wagon:
(define-generator (e-spigot n)
(define (times10 x) (* 10 x))
(yield 2)
(let loop1 ((ts (make-list n 10)) (k n))
(if (< 1 k)
(let loop2 ((ts ts) (rs (list)) (i (+ n 1)) (carry 0))
(if (= i 1)
(begin (yield carry) (loop1 (map times10 (reverse rs)) (- k 1)))
(let* ((x (+ (car ts) carry)) (q (quotient x i)) (r (remainder x i)))
(loop2 (cdr ts) (cons r rs) (- i 1) q))))))
(let loop3 () (yield #f) (loop3)))
The outer loop processes the rows of the tableau one by one, producing another digit each time, and the inner loop processes each row. The third loop returns an indication that processing is complete each time it is called.
The Gibbons algorithm is nearly as pretty in Scheme as in Haskell; like Niemeijer, I made a few changes in translation:
(define-generator (make-spigot f lo hi)
(define (split v)
(values (vector-ref v 0) (vector-ref v 1) (vector-ref v 2)))
(define (approx abc n)
(let-values (((a b c) (split abc)))
(quotient (+ (* a n) b) c)))
(define (mul abc def)
(let-values (((a b c) (split abc)) ((d e f) (split def)))
(vector (* a d) (+ (* a e) (* b f)) (* c f))))
(define (g k) (let-values (((n d a) (f k))) (vector n (* a d) d)))
(let loop ((z (vector 1 0 1)) (k 1))
(let ((lbound (approx z lo)))
(cond ((= lbound (approx z hi))
(yield lbound)
(loop (mul (vector 10 (* -10 lbound) 1) z) k))
(else (loop (mul z (g k)) (+ k 1)))))))
(define pi-spigot (make-spigot (lambda (k) (values k (+ k k 1) 2)) 3 4))
(define e-spigot (make-spigot (lambda (k) (values 1 k 1)) 1 2))
We use a three-slot vector for the tuple, and values
to split tuples into pieces; Scheme’s lack of pattern-matching hurts us here.
We used generators from a previous exercise. You can run the program at http://programmingpraxis.codepad.org/my56jfCf.
[…] today’s Programming Praxis exercise, our goal is to implement two algorithms to calculate the digits of e […]
Here’s my Haskell solution for the first algorithm (since my solution for the second one has already been posted in the exercise). A version with comments can be found at http://bonsaicode.wordpress.com/2012/06/19/programming-praxis-digits-of-e/ .
[…] First we reuse the unbounded spigot algorithm for calculating e from the last exercise; […]
Does anybody share a Java or C# code for this exercises?
Basically a direct translation of the haskell code into Python 2.7. I create the input stream and initialize the state vector (z) in ‘stream()’ and eliminated ‘streamDigits()’.
Here is FORTH code for the first algorithm (by Stan & Stanley) Though space is proportional to n, not sure why you mention n**2.
Hi Mike, when I tried your code it gave me error can’t iterate on a function, fixed the problem by changing the last two lines to
print ”.join(str(d) for d in islice(pi_digits(), 10))
print ”.join(imap(str, islice(e_digits(), 14)))
in other words replacing the function pi_digits with pi_digits() which invokes the function and returns an array, similarly replacin e_digits with e_digits()