Skip to content

Instantly share code, notes, and snippets.

@suryagaddipati
Created August 6, 2010 20:02
Show Gist options
  • Select an option

  • Save suryagaddipati/511908 to your computer and use it in GitHub Desktop.

Select an option

Save suryagaddipati/511908 to your computer and use it in GitHub Desktop.
(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