1.38 Exercise 1.38

Using an internally-defined denom function, which reduces the index by 2 if it’s greater than 2 to make the logic simpler, we can use cont-frac to approximate e using Euler’s expansion like this:

(define (euler-e-frac k)
  (define (denom i)
    (if (< i 3) i
        (let ((red-i (- i 2)))
          (if (= (remainder red-i 3) 0)
              (+ 2 (* 2 (/ red-i 3)))
              1))))
  (+ 2 (cont-frac (lambda (i) 1.0)
                  denom
                  k)))

Testing this procedure, we can see it does calculate e:

> (euler-e-frac 10)

2.7182817182817183

> (euler-e-frac 100)

2.7182818284590455