Posted on 2007-05-17 11:34
dennis 閱讀(722)
評論(0) 編輯 收藏 所屬分類:
計算機科學與基礎
搞定了工作,繼續做習題:)
題1.37:無窮連分式的過程描述,我發現這道題用迭代比遞歸反而更容易寫出來,遞歸不是那么顯而易見。
遞歸版本:
(define (cont-frace n d k)
(if (= k 1)
(/ (n 1) (d 1))
(/ (n k) (+ (d k) (cont-frace n d (- k 1))))))
再看迭代版本:
(define (cont-frace-iter n d result counter k)
(if (= counter 0)
result
(cont-frace-iter n d (/ (n counter) (+ (d counter) result)) (- counter 1) k)))
(define (cont-frace n d k)
(cont-frace-iter n d 0 k k))
當n d的過程都是(lambda (x) 1.0)時,趨近于1/φ(黃金分割比的倒數),通過計算可得知,當k>=11時,滿足十進制的4位精度。
題1.38在1.37的基礎上,關鍵在于寫出d過程,通過觀察給出的序列可以發現,當i-2是3的倍數時,(d i)應該返回2(i+1)/3,由此先寫出d過程:
(define (d i)
(cond ((= i 1) 1)
((= i 2) 2)
((= (remainder (- i 2) 3) 0) (/ (* 2 (+ i 1)) 3))
(else
1)))
據此求出e:
(+ 2 (cont-frace (lambda(i) 1.0) d 1000))