SICP Exercise 1.8
Question
Newton’s method for cube roots is based on the fact that if y
is an approximation to the cube root of x
, then a better approximation is given by the value
$$\frac{x/y^2+2y}{3}$$
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square-root and cube-root procedures.)
Answer
(define (good-enough? guess x)
(< (abs (- (* guess guess guess) x)) (/ x 1000000.0)))
(define (square x)
(* x x))
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess))
3))
(define (cubicrt-iter guess x)
(if (good-enough? guess x)
guess
(cubicrt-iter (improve guess x) x)))
(define cubicrt x)
(cubicrt-iter 1.0 x))
(cubicrt 125)
; returns 5.000000000287929