Project Euler Problem 016
http://projecteuler.net/index.php?section=problems&id=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の1000乗の各桁の合計。
** 演算子を忘れていて math.pow を使っていた。整数値の乗数を計算するには ** を使った方が良さそう。
import datetime def sum_of_digits(n): result = 0 while n: result += n % 10 n /= 10 return result begin = datetime.datetime.now() print sum_of_digits(2 ** 1000) end = datetime.datetime.now() print end - begin
答え: 1366
実行時間: 0.001116秒くらい