Books I Like

Sunday, January 3, 2010

Solving Project Euler Problem #16 in Clojure

Problem Statement :

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?


Solution :
The easiest way to multiply a number by 2 is to perform a left bit shift.
Get the number, convert it to string, put the indices in a map and apply a "+" function on it.


(println (apply + (map #(Integer/parseInt (str %1)) (str (bit-shift-left 2 999)))))

Get at Amazon