Project Euler Problem 56

Ivar Thorson bio photo By Ivar Thorson

This was definitely too easy. Problem 56 once again demonstrates how simple it is to play with bignums and strings.

(use '[clojure.contrib.math :only (expt)])

(defn digit-sum [n] (reduce + (map #(Character/getNumericValue %) (str n))))

(defn euler-56 []
  (reduce max (for [a (range 100)
                    b (range 100)]
                (digit-sum (expt a b)))))

(time (euler-56)) ;; "Elapsed time: 5568.29599 msecs"

We could probably speed this up if we studied the math more (regarding what and how many digits would be produced by exponentiation), but I like the brevity and simplicity of this solution.