Problem 4: Largest palindrome product¶
Link to the problem: https://projecteuler.net/problem=4.
Problem 4
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.
A simple solution¶
Let our palindrom be \(P = a \cdot b\).
\(a\) and \(b\) being 3-digit long, they must lie between \(100\) and \(999\).
Then once we have calculated the product we just need to compute the reverse number (i.e. where all digits are reversed) and find that it is indeed the same number when a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
[info] [2023-02-24 14:01:41.520] [T-876] [logging.cpp::62] [main-wire] Answer: 906609
A better solution¶
We can improve the previous implementation in two ways:
- halving the calculations
- counting downwards since we are looking for the largest palindrome
Halving the calculations comes from the fact that once we have checked for instance for \(a = 123\) and \(b = 456\), we don't need to check again for \(a = 456\) and \(b = 123\). In other words, we could assume \(a \leq b\).
Counting downwards means we start at \(999\) instead of \(100\) and we can stop once the product of \(a \cdot b\) is less than the largest palindrome we found so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
[info] [2023-02-24 14:01:41.611] [T-6232] [logging.cpp::62] [main-wire] Answer: 906609
A much better solution¶
This is all good but we can improve even further. Let the digits of \(P\) be \(x\), \(y\) and \(z\). Since \(P\) is a palindrome, we have:
Since \(11\) is prime, at least one of the numbers \(a\) or \(b\) must have a factor of \(11\). In other words if \(a\) is not divisible by \(11\) then we know \(b\) necessarily must be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
[info] [2023-02-24 14:01:41.696] [T-2896] [logging.cpp::62] [main-wire] Answer: 906609
More details on the Project Euler's website: https://projecteuler.net/overview=004.