06.17.08

Project Euler: Problem 13

Posted in python tagged , at 4:45 pm by redochre

Problem 13:

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250

20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690

A solution:

Copy the list of numbers and save them in a text file (here, Euler13number.txt). Then things are pretty easy, thanks to Python’s magic handling of large numbers.

file = open('Euler13number.txt', 'r')

a = [int(line) for line in file]
b = str(sum(a))
print b[:10]

file.close()

Or, for an even more condensed version:

file = open('Euler13number.txt', 'r')
print str(sum([int(line) for line in file]))[:10]
file.close()

Leave a Comment