用scheme求解立方根,迭代算法

开始读点函数式语言的书,当前主要是SICP。

主要的公式是(x)^{1/3}=\frac{x/y^2+2y},写成迭代的过程为

(define (cube-inverse x)
  (define (good-enough? guess)
    (< (abs (- (/ (* guess guess guess) x) 1)) 0.0001))
  (define (improve guess)
    (/  (+ (/ x (* guess guess)) (* guess 2)) 3))
  (define (cube-inverse-iter guess)
    (if (good-enough? guess)
      guess
      (cube-inverse-iter (improve guess))))
  (cube-inverse-iter 1.0))

 

posted on 2012-09-14 06:11  mathlover  阅读(288)  评论(0编辑  收藏  举报

导航