Created
October 1, 2011 19:41
-
-
Save tedpennings/1256545 to your computer and use it in GitHub Desktop.
"power of" in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn power-of? [power number] | |
"Returns true if a number is a power of another number, eg, | |
(power-of? 5 25) => true | |
(power-of? 5 26) => false" | |
(= | |
(mod | |
(/ | |
(Math/log number) | |
(Math/log power) ) | |
1 ) | |
0 ) | |
) | |
(def power-of? | |
; memoize power-of to cache results | |
(memoize power-of?) | |
) | |
(defn power-of-2? [x] | |
"Returns true if a number is a power of 2" | |
(power-of? 2 x) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment