Skip to content

Instantly share code, notes, and snippets.

@mohanrajendran
Created July 9, 2014 12:41
Show Gist options
  • Save mohanrajendran/e43e149cd04b845ff5ae to your computer and use it in GitHub Desktop.
Save mohanrajendran/e43e149cd04b845ff5ae to your computer and use it in GitHub Desktop.
SICP Exercise 1.8
(define (cbrt-iter guess prev-guess x)
(if (good-enough? guess prev-guess)
guess
(sqrt-iter (improve guess x)
guess
x)))
; cbrt-iter
(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
; improve
(define (good-enough? guess prev-guess)
(< (abs (- guess prev-guess)) 0.001))
; good-enough?
(define (cbrt x)
(cbrt-iter 1.0 0.0 x))
; cbrt
; Test cases:-
(cbrt 0.000001)
; 1.0039485733107844e-2
(cbrt 1e15)
; 10000.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment