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.

 
(define (cont-frac n d k oper)
  (define (cont-frac-inner n d k i)
    (if (= i k) (/ (n i) (d i)) (/ (n i) (oper (d i) (cont-frac-inner n d k (+ i 1))))))
  (cont-frac-inner n d k 0))
 
 
 
(define (seriesx x)
  (define (iter a b last-pair i)
    (if (= i x)
	b
	(if (= a b 1) 
	    (iter b (+ last-pair 2) (+ last-pair 2) (+ i 1)) 
	    (iter b 1 last-pair (+ i 1))))) 
  (iter 1 1 0 0))
 
(define (e x) 
  (+ 2 (cont-frac (lambda (i) 1.0) seriesx x +)))
 
exercise_1.38.txt · Last modified: 2009/03/02 01:51 by mariorz
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki