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))))
(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
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!