Skip to content

Instantly share code, notes, and snippets.

@tedpennings
Created October 1, 2011 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedpennings/1256545 to your computer and use it in GitHub Desktop.
Save tedpennings/1256545 to your computer and use it in GitHub Desktop.
"power of" in Clojure
(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