(defn exact-integer-sqrt "(exact-integer-sqrt n) expects a non-negative integer n, and returns [s r] where n = s^2+r and n < (s+1)^2. In other words, it returns the floor of the square root and the 'remainder'.
For example, (exact-integer-sqrt 15) is [3 6] because 15 = 3^2+6."
[n]
(if (or (not (integer? n)) (neg? n))
(throw (IllegalArgumentException. "exact-integer-sqrt requires a non-negative integer"))
(let [isqrt (integer-sqrt n),
error (- n (* isqrt isqrt))]
[isqrt error])))
Used in 0 other vars
Comments top
No comments for exact-integer-sqrt. Log in to add a comment.