PyPE solution to PE problem 50
PyPE solution to PE problem 50
Blueprint information
- Status:
- Complete
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
-
Implemented
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
- Scott Armitage
Related branches
Related bugs
Sprints
Whiteboard
ProjectEule
===
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
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?
Solution
--------
There is a curiousity between this problem and most others involving prime
numbers -- we don't know how many primes we are going to need, nor the
upper-limit on the value of primes. Instead, we use a less-efficient lazy
prime generator, and track the sum of all primes. Once that sum goes over
the limit sum, we know that no other prime numbers are required and we can
break from the loop.
Next, we iterate down from using the most number of elements, and we iterate
from highest to lowest elements. Once we find a sum that is a prime number,
this is the maximum (since we are counting down in both respects), so we
can just return this number. If we get to the end of the prime list and
still haven't found anything, return None to signal an error.
Answer
------
997651