Clojure Euler: Problem 020

Find the sum of the digits in the number 100!


First of all we need to calculate factorial. Without integer overflows, stack overflows and other caveats.

Previous article Fast Factorial has working solution that we might use.

(defn ! [n]
  (reduce *' (range 1 (inc n))))

Now, calculate the sum of digits, the same function that we used in Clojure Euler: Problem 008 and Clojure Euler: Problem 016

(defn sum-of-digits [n]
  (reduce + (map #(- (int %) 48) (seq (str n)))))

And the final line

(sum-of-digits (! 100))

Simple enough, huh?


P.S. The functions sum-of-digits, count-digits and similar used often and look very ugly, so I decided to create a separate set of functions (library?) which I can refer. It should be a set of common mathematic functions, including different sequences, sums, converters, interesting numbers. You might use it too. Welcome to numberto!

mishadoff 21 July 2013
blog comments powered by Disqus