Project Euler Problem 48

Ivar Thorson bio photo By Ivar Thorson

Another one that is too easy in clojure. Problem 48 wants us to find the sum of 1^1 + 2^2 + … + 1000^1000. With Clojure’s built-in bignum support, this is a cinch:

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

(defn euler-48 []
  (let [d (reduce + (map #(expt % %) (range 1 1001)))]
    (BigInteger. (apply str (reverse (take 10 (reverse (str d))))))))

This takes less than 100ms to compute on my machine, so there is no need to bother optimizing it.