Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn exp
- "exponent of x^n (int n only), with tail recursion and O(logn)"
- [x n]
- (if (< n 0)
- (/ 1 (exp x (- n)))
- (loop [acc 1
- base x
- pow n]
- (if (= pow 0)
- acc
- (if (even? pow)
- (recur acc (* base base) (/ pow 2))
- (recur (* acc base) base (dec pow)))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement