Created
August 6, 2010 20:02
-
-
Save suryagaddipati/511908 to your computer and use it in GitHub Desktop.
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
| (ns sqrt) | |
| (defn abs [n] | |
| (if (< n 0) | |
| (* -1 n)n)) | |
| (defn goodenough? [guess prevguess] | |
| (> 0.0000001 ( abs (- guess prevguess) ) )) | |
| (defn sqrt-approximation [x y] | |
| ( / (+ y (/ x y)) 2.0)) | |
| (defn cubert-approximation [x y] | |
| (/ (+ (/ x (Math/pow y 2)) (* 2 y))3) ) | |
| (defn root | |
| ([ approximation x ] (root x 1 0 approximation) ) | |
| ( [x guess prevguess approximation] | |
| ( if (goodenough? guess prevguess ) | |
| guess | |
| ( recur x (approximation x guess) guess approximation)))) | |
| (def sqrt (partial root sqrt-approximation )) | |
| (def cubert (partial root cubert-approximation )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment