Problem 16

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?

2^15 = 32768 であり,その各桁の数の和は 3 + 2 + 7 + 6 + 8 = 26 である.

では,2^1000の各桁の和を求めよ.

Haskell

シンプルに各桁の数字を求めればいいです.

{-
Project Euler Problem 16 e16.hs
-}

g:: Integer -> [Integer]
g n | n >= 10   = r:(g ((n-r) `div` 10))
    | otherwise = [n]
    where r = n `mod` 10

answer = sum.g

答えは

*Main> answer (2^1000)
1366
(0.02 secs, 524936 bytes)