Project Euler 50: Find the prime number, below one-million, that can be written as the sum of the most consecutive primes.
Project Euler 50 Problem Description
Project Euler 50: The prime 41, can be written as the sum of six consecutive primes:
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
We need to answer two simple questions: With which prime do we start and how many consecutive primes do we need to add.
We built an array of sums, each time adding a new prime to the total until the total exceeded the maximum (1,000,000 in this case.) It is then a simple task to look at different sums and record the one with the most terms.
Project Euler 50 SolutionRuns < 0.003 seconds in Python 2.7.
Use this link to get the Project Euler 50 Solution Python 2.7 source.
AnswerSlowly 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.
- Was surprised by how many terms were required.
- Here is a good discussion for prime sums
- Table of test values
|Limit||Starting prime||Prime sum||# terms||Run time|
|102||2||41||6 Terms||<0.001 sec.|
|103||7||953||21 Terms||<0.001 sec.|
|104||3||9521||65 Terms||<0.001 sec.|
|105||3||92951||183 Terms||<0.001 sec.|
|106||7||997651||543 Terms||0.002 sec.|
|107||5||9951191||1587 Terms||0.005 sec.|
|108||7||99819619||4685 Terms||0.011 sec.|
|109||11||999715711||13935 Terms||0.03 sec.|
|1010||2||9999419621||41708 Terms||0.1 sec.|
|1011||19||99987684473||125479 Terms||0.36 sec.|
|1012||5||999973156643||379317 Terms||1 sec.|