Created
July 9, 2014 12:41
-
-
Save mohanrajendran/e43e149cd04b845ff5ae to your computer and use it in GitHub Desktop.
SICP Exercise 1.8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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