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)?
http://projecteuler.net/problem=16
2^(1000)の各桁の和を求める問題。
力任せに書くと次のようになるか:
----
sum(map(int,str(pow(2,1000))))
----
まずpow(2,1000)で2の1000乗を計算し、それを文字列型に変換する。そしてmap函数でリストの成分に対してint函数で整数型にする。最後にsum函数で和を求める。
別の方法も一応考えた。
----
ans = 0
for x in str(pow(2,1000)):
ans += int(x)
print ans
----
pow(2,1000)で2の1000乗を計算しそれを文字列型にするまでは同じであるが、文字列上をfor文で回す。
どちらも計算時間は対して変わらないが前者のほうが若干早い。