SICP exercise 1.38
From Drewiki
Problem
In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, which included a continued fraction expansion for e - 2, where e is the base of the natural logarithms. In this fraction, the Ni are all 1, and the Di are successively 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, .... Write a program that uses your cont-frac procedure from exercise 1.37 to approximate e, based on Euler's expansion.
Solution
We need a function d which produces the pattern for the Di:
| i | Di |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 4 | 1 |
| 5 | 4 |
| 6 | 1 |
| 7 | 1 |
| 8 | 6 |
| 9 | 1 |
| 10 | 1 |
| 11 | 8 |
The pattern has a period of 3: 1, x, 1. Di is 1 if i mod 3 is either 0 or 1. Otherwise, Di is
. Several popular Scheme implementations provide a quotient primitive that'll give us the whole number part of i/3:
Let's test this function before we go on:
Output:
1
Output:
2
Output:
1
Output:
1
Output:
4
Output:
1
Output:
1
Output:
6
Output:
1
Output:
1
Output:
8
That looks good. Now let's use it to compute an approximation of e,
using k terms and the cont-frac function from exercise 1.37
(remember that the continued fraction expansion published by Euler is for e - 2):
And now let's test it for k = 10 and k = 20. e is approximately 2.71828183. The results given below were generated using Chicken Scheme 3.1 on a MacBook Pro running OS X 10.5.
Output:
2.71828171828172
Output:
2.71828182845905
Note that even 10 steps are sufficient to approximate e to 7 digits.

