## Question

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

\displaystyle \begin{aligned} 1634&=1^4 + 6^4 + 3^4 + 4^4 \\ 8208&=8^4 + 2^4 + 0^4 + 8^4 \\ 9474&=9^4 + 4^4 + 7^4 + 4^4 \end{aligned}

As $1=1^4$ is not a sum it is not included.

The sum of these numbers is $1634+8208+9474=19316$.

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))))
$time clojure fifth-powers.clj real 0m1.825s user 0m2.596s sys 0m0.132s ## 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]
$ghc -O2 -o sumFifths sumFifths.hs$ time ./sumFifths
real   0m0.435s
user   0m0.432s
sys    0m0.000s

## Ruby

#!/usr/bin/env ruby
puts (2..500000).select { |i|
i == i.to_s.each_char.map { |d| d.to_i**5 }.reduce(:+)
}.reduce(:+)
\$ time ruby sum-of-fifths.rb
real   0m1.366s
user   0m1.352s
sys    0m0.004s