PyPE solution to PE problem 4
PyPE solution to PE problem 4
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
Whiteboard
ProjectEule
===
A palindromic number reads the same both ways. The largest palindrome made
from the product of two 2-digit numbers is 9009 = 91 * 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Solution
--------
If we call the two numbers i and j, then we first find the upper and lower
bounds for i and j. For 3-digit numbers, these are 999 and 100 respectively.
We choose to iterate between these bounds reversely, such that once we reach
a value of p=i*j that is less than the maximum palindrome so far, we break
out of the loop as all subsequent iterations will be smaller again.
We also choose our minor iteration of j to begin at i; this way we prevent
double-checking values such as i=132, j=528 vs i=528, j=132.
Lastly, we note that the largest possible multiple (999*999=998001) is six
digits long. We also note that 111111 is a palindrome, and thus the solution
to the puzzle must lie between these two values (i.e. that it is a six-digit
number. In order for a 6-digit number to be palindromic, it must have at most
three distinct digital values, which we will label a, b, and c. By analysis,
we know that any solution will be of the form
p = 100000*a + 10000*b + 1000*c + 100*c + 10*b + a
= 100001*a + 10010*b + 1100*c
= 11 * (9091*a + 910*b + 100*c)
We now know that at least one of i or j must have a factor of 11, and so we
can use this knowledge to check only such combinations (i.e. if i does not
have a factor of 11, then we set j to the largest multiple of 11 less than
or equal to i and reverse-iterate j by steps of 11).
Answer
------
906609