SICP 全笔记

Exercise 1.38. 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.

根据描述,Euler’s continued fraction 公式为:

$$e - 2 = \frac{1}{1+\frac{1}{2+\frac{1}{1+\frac{1}{1+\frac{1}{4+\dots}}}}} $$ 但这个公式不适合于我们的 cont-frac 过程,因为序列 1, 2, 1, 1, 4, 1, 1, 6……不容易从用函数来描述项数与值的关系。

我们可以变一下,这样来想这个公式

fix this

$$e - 1 = \left\( \frac{1}{1+\frac{1}{1+\frac{1}{2+\frac{1}{1+\frac{1}{1+\frac{1}{4+\dots}}}}}} \right\)^{-1} $$ 这样,我们的过程变为需要处理 1, 1, 2, 1, 1, 4, 1, 1, 6……方便地处理 2 4 6 等数–它们都为 (2i)/3。

(load "37-cont-frac.scm")
(load "../testframe.scm")
(let ((e-1
       (/ 1
          (cont-frac (lambda (i) 1.0)
                     (lambda (i)
                       (if (= (remainder i 3) 0)
                           (/ (* 2 i) 3)
                           1))
                     100)))
      (e 2.7182818284590452353602874713))
  (display (+ e-1 1))) ;Value: 2.7182818284590455

最后我们可以对 e - 1 求值,这个值加上 1 就是 e 的近似值

;Value: 2.7182818284590455