SICP Exercise 1.38
Question
In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, which included a continued fraction expansion for cont-frac
procedure from exercise 1.37 to approximate
Answer
The equation that I used for coming up with
First of all, if
So, here is the full code.
Again, we need surprisingly few iterations (only 7) to get to an accuracy of 4 decimal places (
(define (d i)
(if (not (= 0
(remainder (- i 2) 3))) 1
(+ 2 (* 2 (/ (- i 2) 3)))))
(define (cont-frac n d k)
(define (iterate counter)
(if (= k counter) (/ (n k) (d k))
(/ (n counter) (+ (d counter) (iterate (+ counter 1))))))
(iterate 1))
(+ 2 (cont-frac (λ (i) 1.0)
d
7))
Result:
2.7183098591549295