PyPE solution to PE problem 49
PyPE solution to PE problem 49
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 arithmetic sequence, 1487, 4817, 8147, in which each of the terms
increases by 3330, is unusual in two ways: (i) each of the three terms are
prime, and, (ii) each of the 4-digit numbers are permutations of one
another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit
primes, exhibiting this property, but there is one other 4-digit increasing
sequence.
What 12-digit number do you form by concatenating the three terms in this
sequence?
Solution
--------
We know we are looking for prime numbers, so we pre-compute a list of all
prime numbers up to 10,000 (since the largest number of interest will be
9,999), and store a boolean for primality in an array (for fast indexing).
We then loop over all 4-digit primes (the first one being index 168 in the
list of primes). For each starting prime in the sequence, we loop through
all the possible arithmentic increases. For each one, we compute the second
number in the sequence; if the second number is prime, we compute the third
number in the sequence; if the third number is prime, we continue our more
extensive -- and more expensive -- tests.
The next test is to see if the digits of y (the second number in the
sequence) are some permutation of the digits of x. If so, then we also
check the digits of z. The problem statement tells us that there are only
two 4-digit sequences, and so we skip the one we are already aware of, and
return an answer the next time we find something.
Note that when looping over differences, in order for all numbers to be
prime, the difference must be even -- otherwise one of the numbers in the
sequence (specifically, the middle one) will end up being even, and thus
definitely not prime.
Answer
------
296962999629