Skip to content

Instantly share code, notes, and snippets.

@pyrocat101
Created October 6, 2013 11:28
Show Gist options
  • Save pyrocat101/6852861 to your computer and use it in GitHub Desktop.
Save pyrocat101/6852861 to your computer and use it in GitHub Desktop.
(defn nth-root
[x n]
(Math/pow Math/E (/ (Math/log x) n)))
(defn sum-of-powers
[sum n start]
(let [upper-bound (int (inc (nth-root sum n)))]
(->> (range start (inc upper-bound))
(map
(fn [x]
(let [remain (- sum (int (Math/pow x n)))]
(cond (zero? remain) 1
(neg? remain) 0
:else (sum-of-powers remain n (inc x))))))
(reduce +))))
@pyrocat101
Copy link
Author

(sum-of-powers 100 3 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment