Friday, June 20, 2014

Project Euler - Power digit sum - Problem 16 - Solution in Clojure

Power digit sum

Problem 16

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?


as
Let us first write a power function

user=> (defn pow [ a b]
  #_=>   (reduce *' (repeat b a)))
#'user/pow
user=> (pow 2 15)
32768
user=> (pow 2 100)
1267650600228229401496703205376N
user=> (pow 2 1000)
10715086071862673209484250490600018105614048117055336074437503883703510511249361
22493198378815695858127594672917553146825187145285692314043598457757469857480393
45677748242309854210746050623711418779541821530464749835819412673987675591655439
46077062914571196477686542167660429831652624386837205668069376N

And then the rest of the action is packed into -

(reduce + (map #( - (int %) 48) (str (pow 2 1000))) )

The answer is 1366.

No comments:

Post a Comment