SCIP 1.8
Exercise 1.8. 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
(/ (+ (/ x (squre y))
(* y 2))
3)
(x/(y^2)+2y)/3
Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In
section 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.)
(defun squre(x)
(* x x))
(defun mysqrt_ex(x)
(sqrt-iter-ex 1.0 x))
(defun improve_c (guess x)
(/ (+ (/ x (squre guess))
(* 2 guess))
3))
(defun good-enough_c (guess x)
(< (abs (/ (- guess (improve_c guess x))
guess
))
0.0000001))
(defun cbrt-iter-ex (guess x)
(if (good-enough_c guess x)
guess
(cbrt-iter-ex (improve_c guess x)
x)))
(defun cuberoot(x)
(cbrt-iter-ex 1.0 x))
CL-USER> (cuberoot (* 64.0 8))
8.0
Edit in Emacs with Slime and Steel Bank Common Lisp.