Skip to content

Instantly share code, notes, and snippets.

@bendlas
Created December 4, 2011 16:17
Show Gist options
  • Save bendlas/1430574 to your computer and use it in GitHub Desktop.
Save bendlas/1430574 to your computer and use it in GitHub Desktop.
(defn fast-expt [b n]
(loop [acc 1
b b
n n]
(cond
(= n 0) acc
(even? n) (recur acc (* b b) (/ n 2))
:else (recur (* acc b) b (dec n)))))
(defn mult [a b]
(loop [acc 0
a a
b b]
(cond
(= b 1) (+ a acc)
(even? b) (recur acc (* a 2) (/ b 2))
:else (recur (+ a acc) a (dec b)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment