Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. (defn exp
  2. "exponent of x^n (int n only), with tail recursion and O(logn)"
  3. [x n]
  4. (if (< n 0)
  5. (/ 1 (exp x (- n)))
  6. (loop [acc 1
  7. base x
  8. pow n]
  9. (if (= pow 0)
  10. acc
  11. (if (even? pow)
  12. (recur acc (* base base) (/ pow 2))
  13. (recur (* acc base) base (dec pow)))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement