Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.scheme

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-11-2012, 12:57 AM
.
Guest
 
Posts: n/a
Default SICP: Exercise 1.38: Euler's number

Hi,

"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." [1]

Solution: [2]

(define (cont-frac n d k)
**(define (iter i sub-expr)
****(if (= i 0)
********sub-expr
********(iter (- i 1) (/ (n i) (+ (d i) sub-expr)))))
**(iter (- k 1) (/ (n k) (d k))))

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

This is a part which I don't understand:

(if (= (remainder (+ i 1) 3) 0)
**************************** (* 2 (/ (+ i 1) 3))

I'm not really good at math that's why I looked through some articles.
But I haven't found anything similar to this conditions.
Where did we get them?

[1] http://mitpress.mit.edu/sicp/full-te...ml#%_sec_1.3.2
[2] http://www.kendyck.com/archives/2007...p-exercise-138

Thanks
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-15-2012, 03:53 AM
toby
Guest
 
Posts: n/a
Default Re: SICP: Exercise 1.38: Euler's number

On Friday, 10 August 2012 20:57:14 UTC-4, . wrote:
> Hi,
>
>
>
> "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." [1]
>
>
>
> Solution: [2]
>
> ...
> This is a part which I don't understand:
>
> (if (= (remainder (+ i 1) 3) 0)
> *** (* 2 (/ (+ i 1) 3))
>


This expression generates the denominators for i=0,1,...

The required values from the problem statement are:
1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, (Di)
Notice that these are all 1 except for every third value after the 2nd. Visualising the sequence starting from i=0,1,..., we can see:
i = 0 1 2 3 4 5 6 7 8 9 10 ...
i+1 = 1 2 3 4 5 6 7 8 9 10 11 ...
(i+1)%3 = 1 2 0 1 2 0 1 2 0 1 2 ... (remainder, r)
(i+1)/3 = 0 0 1 1 1 2 2 2 3 3 3 ... (quotient, q)
if we choose 2*q when r == 0, and 1 otherwise, we obtain the desired Di..

(My solution is here http://www.telegraphics.com.au/svn/p.../sicp/1-38.scm )

> ...
> [1] http://mitpress.mit.edu/sicp/full-te...ml#%_sec_1.3.2
>
> [2] http://www.kendyck.com/archives/2007...p-exercise-138
>
>
>
> Thanks


Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:11 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.