$2^{15} = 32768$ and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number $2^{1000}$?
There is probably a fancy way to do this using the properties of powers and modulo. However since Clojure supports bigints, we can just brute force this one by setting a variable to $2^1000$ and then just summing the digits of it:
(use 'clojure.math.numeric-tower)
(defn calculate [power]
(loop [n (expt 2 power)
total 0]
(if (== n 0)
total
(recur (quot n 10)
(+ total (mod n 10))))))
This runs very fast:$ lein run Processing... 1366N "Elapsed time: 5.569124 msecs"
No comments:
Post a Comment