Skip to main content.
-->
March 20th, 2005

Solution to SICP Exercise 1.8

Structure and Interpretation of Computer Programs

Solution to Exercise 1.8:

(define (square x)
  (* x x))

(define (cube x)
  (* x x x))

(define (cbrt-iter guess prev-guess x)
  (if (good-enough? guess prev-guess)
      guess
      (cbrt-iter (improve guess x)
                 guess
                 x)))

(define (improve guess x)
  (/ (+ (/ x (square guess))
        (* 2 guess))
     3))

(define (good-enough? guess prev-guess)
  (< (/ (abs (- guess prev-guess))
        guess)
     0.001))

(define (cbrt x)
  (cbrt-iter 1.0 0.0 x))

This solution uses the guess evaluation strategy of Exercise 1.7.

It seems to give reasonable results:

> (cbrt 8)
2.000000000012062
> (cbrt 27)
3.0000005410641766
> (cbrt 64)
4.000000000076121

Posted by Ken Dyck in Programming

No Comments »

This entry was posted on Sunday, March 20th, 2005 at 9:38 am and is filed under Programming. You can follow any responses to this entry through the comments RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>