练习1.8 求立方根的牛顿法基于如下事实,如果y是x的立方根的一个近似值,那么下式将给出一个更好的近似值:
y=x/y2+2y3
请利用这一公式实现一个类似平方根过程的求立方根的过程。
#lang scheme
;;; SICP execise 1.8 get the cube root
(define (square x)
(* x x))
(define (cube x)
(* x x x))
(define (crt x)
(crt-iter 1.0 x))
(define (crt-iter guess x)
;;; (println "crt-iter")
;;; (println guess)
;;; (println x)
(if (guess-enough? guess (improve guess x))
(improve guess x)
(crt-iter (improve guess x) x)))
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
(define (guess-enough? old-guess new-guess)
(< (/ (abs (- new-guess old-guess)) old-guess) 0.0000000001))
(crt 9)
(crt 81)
(crt 1000)