{"content":{"id":261,"title":"SICP Solutions - 1.8","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e 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:\r\n\u003c/p\u003e\r\n\u003cimg src=\"/uploads/c7.gif\" class=\"card-image\"\u003e\r\n\u003cp\u003e\r\nUse 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.)\r\n\u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e \r\n\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e;; The setup is the same, except for the names.\r\n(define (cbrt-iter guess x)\r\n (if (good-enough? guess x)\r\n guess\r\n (cbrt-iter (improve guess x)\r\n x)))\r\n\r\n(define (cube x)\r\n (* x x x))\r\n\r\n(define (good-enough? guess x)\r\n (\u003c (abs (- (cube guess) x)) 0.001))\r\n\r\n(define (cbrt x)\r\n (cbrt-iter 1.0 x))\r\n\r\n;; The formula in scheme.\r\n(define (improve guess x)\r\n (/ \r\n (+ (/ x (* guess guess)) (* 2 guess))\r\n 3))\r\n\r\n;; A few guesses.\r\n(display (cbrt 8))\r\n(newline)\r\n(display (cbrt 27))\r\n(newline)\r\n(display (cbrt 60))\r\n(newline)\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e2.0000049116755\r\n3.00000054106418\r\n3.91487458417134 ; My calculator gives 3.91486764117\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-09-15T03:00:00.000Z","created_at":"2018-09-08T03:13:03.000Z","updated_at":"2019-01-16T02:28:24.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=sicp\"\u003esicp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=lisp\"\u003elisp\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=scheme\"\u003escheme\u003c/a\u003e"}

SICP Solutions - 1.8

Q: 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:

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.)


A:

;; The setup is the same, except for the names.
(define (cbrt-iter guess x)
  (if (good-enough? guess x)
    guess
    (cbrt-iter (improve guess x)
               x)))

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

(define (good-enough? guess x)
  (< (abs (- (cube guess) x)) 0.001))

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

;; The formula in scheme.
(define (improve guess x)
  (/ 
    (+ (/ x (* guess guess)) (* 2 guess))
    3))

;; A few guesses.
(display (cbrt 8))
(newline)
(display (cbrt 27))
(newline)
(display (cbrt 60))
(newline)
2.0000049116755
3.00000054106418
3.91487458417134 ; My calculator gives 3.91486764117