Euler #13: Large Sum

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

https://projecteuler.net/problem=13

Go to the link if you really want to see all 100 numbers. The solution is easy with Python because of its dynamic typing. The solution is tougher in other languages because the default data types such as int or long aren’t big enough to hold a 50-digit number. A 64-bit number can go up to 2^{64} which is approximately 10^{19}. A 50-digit number goes up to 10^{50}. The primitive data types are no match for the 50-digit numbers.

Python handles this with dynamic typing. It can adjust the “container” on the fly for really, really large numbers. This flexibility comes at the expense of speed and correctness. Dynamic typing can open oneself up to all sorts of bugs. It also incurs overhead when Python has to reallocate memory. It’s good to keep these limitations in mind.

Anyway, the solution to this with Python is pretty easy. First, ingest the file containing the numbers. Second, add ’em together. Third, spit out the result and celebrate!

total = 0
with open('numbers.txt') as f:
    for line in f:
        total += int(line)
print(total) # Full sum
print(str(total)[0:10]) # First 10 digits