Question
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
As is not a sum it is not included.
The sum of these numbers is .
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Clojure
#!/usr/bin/env clojure
(defn raise [x n]
(. (. java.math.BigInteger (valueOf x)) (pow n)))
(defn digits [number]
(map #(- (int %) 48) (str number)))
(defn can-be-written-as-sum-of-fifths? [number]
(and (not (= number 1)) (= (apply + (map #(raise % 5) (digits number))) number)))
(println (reduce + (filter can-be-written-as-sum-of-fifths? (range 500000))))
Haskell
digits :: Integer -> [Integer]
digits n = digits' n []
where digits' 0 acc = acc
digits' n acc = digits' (div n 10) (mod n 10 : acc)
main = print $ sum [n | n <- [2..500000], sum (map (^5) (digits n)) == n]