Project Euler #16 on Python -
import math def sumofdigit(num): sum_of_digits = 0 while num > 9: sum_of_digits += num % 10 num = int(num/10) sum_of_digits += num return sum_of_digits print sumofdigit(math.pow(2,1000)) this thought of project euler #16, , works fine smaller numbers. when tried 21000, code gave me 1289.0 instead of answer 1366 (found on internet). think problem might have size of number, i'm not sure. points in right direction appreciated. in advance!
edit: here question https://projecteuler.net/problem=16
the reason math.pow(2,1000) returns float , not int. therefore, operation num/10 returns different answer expected.
when calling 2 ** 1000 expected answer returned.
edit: in case of python 3, please note hans comment or tobias_k answer regarding integers division.
Comments
Post a Comment