Middle Earth

sicp-exercise-1.35-1.39

Exercise 1.35


(define (cal-golden-ratio)
(find-fixed-points (lambda (x) (+ 1 (/ 1 x)))
1.0))


Exercise 1.36


(define (find-fixed-points f x)
(let ((next-guess (f x)))
(cond ((close-enough? x next-guess) x)
(else (newline)
(display x)
(find-fixed-points f next-guess)))))

(find-fixed-points (lambda (x) (/ (log 1000)
(log x)))
2)

>>

2
9.965784284662087
3.004472209841214
6.279195757507157
3.759850702401539
5.215843784925895
4.182207192401397
4.8277650983445906
4.387593384662677
4.671250085763899
4.481403616895052
4.6053657460929
4.5230849678718865
4.577114682047341
4.541382480151454
4.564903245230833
4.549372679303342
4.559606491913287
4.552853875788271
4.557305529748263
4.554369064436181
4.556305311532999
4.555028263573554
4.555870396702851
4.555315001192079
4.5556812635433275
4.555439715736846
4.555599009998291
4.555493957531389
4.555563237292884
4.555517548417651
4.555547679306398
4.555527808516254
4.555540912917957
4.555532270803653
4.555537970114198
4.555534211524127
4.555536690243655
4.555535055574168
4.5555361336081
4.555535422664798
4.5555358915186215
4.555535582318266
4.555535786230128


(find-fixed-points (lambda (x) (average x
(/ (log 1000)
(log x))))
2)

>> far fewer steps

2
5.9828921423310435
4.922168721308343
4.628224318195455
4.568346513136242
4.5577305909237005
4.555909809045131
4.555599411610624
4.5555465521473675
4.555537551999825
4.555536019631145


Exercise 1.37
Iterative one, Calculating from inner-most outward.

(define (cont-frac N D k)
(define (iter ix result)
(if (= ix 0)
result
(iter (- ix 1.0)
(/ (N ix)
(+ (D ix) result)))))
(iter k 0))


Recursive one. Calculating from out-most inward.

(define (cont-frac N D k)
(define (aux ix)
(if (> ix k)
0
(/ (N ix)
(+ (D ix)
(aux (+ ix 1))))))
(aux 1))



(display (/ 1.0
(cont-frac (lambda (x) 1.0)
(lambda (x) 1.0)
12)))

k=12 is ok, accurate to 4 decimal places, 1.6180

Exercise 1.38

Observe that
Di =
1 if i%3 = 0 or 1
2*((i + 1)/3) if i%3 = 2



(define (euler-expansion)
(cont-frac (lambda (x) 1.0)
(lambda (i)
(let ((rem (remainder i 3)))
(if (= rem 2)
(* 2 (/ (+ i 1) 3))
1)))
10))

10-term recursive can do the job well, generating 2.71828171 (e=2.71828183)

Exercise 1.39

Noticing Ni can be expressed as, x, -x^2, -x^2, -x^2, J.H.Lambert’s formula can be evaluated as a finite continued fraction.

(define (tan-cf x)
(cont-frac (lambda (i) (if (= i 1)
x
(- (square x))))
(lambda (i) (- (* 2 i) 1))
10))

Comments