Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created September 18, 2009 20:18

Revisions

  1. jimweirich renamed this gist Sep 18, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jimweirich revised this gist Sep 18, 2009. 1 changed file with 31 additions and 2 deletions.
    33 changes: 31 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,31 @@
    SICP 1.8
    Fork me. Solve me.
    ;; 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
    ;;
    ;; 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.)

    ;; ANSWER ------------------------------------------------------------

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

    ;; New improve formula for cube root.
    (define (improve guess x)
    (/ (+ (/ x (square guess)) (* 2 guess))
    3) )

    ;; New good enough test for cube root. Using the relative epsilon
    ;; code from ex1.7.
    (define (good-enough? guess x)
    (< (abs (- guess (/ x (square guess))))
    (/ guess 1000000)) )

    (define (try guess)
    (if (good-enough? guess x)
    guess
    (try (improve guess x))))

    (try 1.0))
  3. jimweirich created this gist Sep 18, 2009.
    2 changes: 2 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    SICP 1.8
    Fork me. Solve me.