The following uses Newton’s Method to find square root.
It keeps record of how guess changes from one iteration to the next and stops when the change is a very small fraction of the guess.
This file contains hidden or 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 (square x) (* x x)) | |
| (define | |
| (average x y) | |
| (/ | |
| (+ x y) | |
| 2)) | |
| (define | |
| (good-enough? | |
| x | |
| guess) | |
| (< | |
| (abs | |
| (- | |
| x | |
| (square guess))) | |
| 0.00001)) | |
| (define | |
| (good-enough2? | |
| x | |
| guess) | |
| (< | |
| (abs | |
| (- | |
| x | |
| guess)) | |
| 0.00001)) | |
| (define (abs x) | |
| (cond ((> x 0) x) | |
| ((= x 0) 0) | |
| ((< x 0) (- x)))) | |
| (define | |
| (improve-guess | |
| x | |
| guess) | |
| (average | |
| (/ | |
| x | |
| guess) | |
| guess)) | |
| (define | |
| (sqrt-iter x guess) | |
| (if | |
| (or | |
| (good-enough? | |
| x | |
| guess) | |
| (good-enough2? | |
| guess | |
| (improve-guess | |
| x | |
| guess))) | |
| guess | |
| (sqrt-iter | |
| x | |
| (improve-guess | |
| x | |
| guess))))) | |
| (define (sqrt x) (sqrt-iter x 1.0)) | |
Leave a Reply