Created
October 6, 2013 11:28
-
-
Save pyrocat101/6852861 to your computer and use it in GitHub Desktop.
Functional Programming: The Sums of Powers --> https://www.hackerrank.com/challenges/functional-programming-the-sums-of-powers
This file contains hidden or 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 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 +)))) |
Author
pyrocat101
commented
Nov 9, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment