// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (19 votes, average: 5.00 out of 5)
Loading...

Project Euler Solutions

Project Euler 13 Solution

Project Euler 13 Solution

Project Euler 13: Find the first ten digits of the sum of one-hundred 50-digit numbers.


Project Euler 13 Problem Description

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

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
… {data continues}

Analysis

This problem requires us to find the first 10 (leftmost) digits of the sum of one hundred 50-digit integers.

The numbers were saved to a file named pe13.txt (second Trinket tab next to the main.py tab). This keeps the data and the program separate and the process easy to comprehend.

The file is read as strings and converted to a list of integers using the map function with open as the iterator and int as the application function: sum(map(int, open('pe13.txt'))). After the end of the file is reached the list is fed to the sum function which adds the integers together: sum(map(int, open('pe13.txt'))). This integer sum is finally converted to a string str(total)[:10] and truncated to the top 10 digits for a solution: str(total)[:10].

HackerRank increases the data set from 100 to 1000 numbers. Same solution.

Project Euler 13
This program and method
solves all test cases for
Project Euler 13 on HackerRank

Project Euler 13 Solution

Runs < 0.001 seconds in Python 2.7.
download arrowUse this link to get the Project Euler 13 Solution Python 2.7 source.

Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose define to reveal the answer.
|5537376230|

Afterthoughts

  • Also in one line: print "Top 10 digits of sum =", str(sum(map(int, open('pe13.txt'))))[:10]

We had to copy and paste the data from the problem description and save it to a file. Yeah, I know I already mentioned that, but I just need more content so I can get this paged indexed. Anyway, how is everything? Hope you’re doing well and I’m glad you stopped by. Oh, don’t worry, only 30 people in the last 5 years have ever ventured to this page, so our conversation is, more than likely, quite personal and mostly private.

Have a great day – and check out some of my other pages.

Thanks,
your friend,
Mike

June 8, 2014

Project Euler 13 Solution last updated

Discussion

6 Responses to “Project Euler 13 Solution”

  1. Hey, I was wondering how you saved the file? I’m running the same code off repl python and nothing seems to run

    Posted by Eric | October 27, 2017, 7:55 AM

Post a comment