Created
January 16, 2019 20:26
-
-
Save ExpandingShapes/b9a9d7501df874a836a479f812244679 to your computer and use it in GitHub Desktop.
Eval square root by Newton's method
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 (average x y)(/(+ x y) 2)) | |
(define square(lambda (x)(* x x))) | |
(define (my-abs x) | |
(cond ((< x 0)(- x)) | |
((= x 0) 0) | |
((> x 0) x))) | |
(define (my-sqrt x) | |
(define (improve guess) | |
(average guess (/ x guess))) | |
(define (good-enough? guess) | |
(< (my-abs (- (square guess) x)) | |
.001)) | |
(define (try guess) | |
(if (good-enough? guess) | |
guess | |
(try (improve guess)))) | |
(try 1)) | |
(display (my-sqrt 36)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment