1
|
|
|
""" |
2
|
|
|
Project Euler Problem 4: Largest Palindrome Product |
3
|
|
|
=================================================== |
4
|
|
|
|
5
|
|
|
.. module:: solutions.problem4 |
6
|
|
|
:synopsis: My solution to problem #4. |
7
|
|
|
|
8
|
|
|
The source code for this problem can be |
9
|
|
|
`found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem4.py>`_. |
10
|
|
|
|
11
|
|
|
Problem Statement |
12
|
|
|
################# |
13
|
|
|
|
14
|
|
|
A palindromic number reads the same both ways. The largest palindrome made from the product of two :math:`2`-digit |
|
|
|
|
15
|
|
|
numbers is :math:`9009 = 91 \\times 99`. |
16
|
|
|
|
17
|
|
|
Find the largest palindrome made from the product of two :math:`3`-digit numbers. |
18
|
|
|
|
19
|
|
|
Solution Discussion |
20
|
|
|
################### |
21
|
|
|
|
22
|
|
|
Let :math:`x` be one :math:`3`-digit number and let :math:`y` be the other |
23
|
|
|
|
24
|
|
|
Observe that the product of two :math:`3`-digit numbers will be either a :math:`5` or :math:`6` -digit number. Further, |
|
|
|
|
25
|
|
|
if such numbers were palindromes, they could be expressed in base :math:`10` in one of the following two ways: |
|
|
|
|
26
|
|
|
|
27
|
|
|
.. math:: |
28
|
|
|
|
29
|
|
|
abcba &= & 10000 \\times a &+ 1000 \\times b &+ 100 \\times c &+ 10 \\times b &+ a \\\\ |
|
|
|
|
30
|
|
|
abccba &= 100000 \\times a +& 10000 \\times b &+ 1000 \\times c &+ 100 \\times c &+ 10 \\times b &+ a \\\\ |
|
|
|
|
31
|
|
|
|
32
|
|
|
Where :math:`a,b,c` are decimal digits. |
33
|
|
|
|
34
|
|
|
Since we are looking for a maximal product, we will assume that it is a :math:`6`-digit one. Observe that: |
|
|
|
|
35
|
|
|
|
36
|
|
|
.. math:: |
37
|
|
|
|
38
|
|
|
abccba &= 100000 \\times a + 10000 \\times b + 1000 \\times c + 100 \\times c + 10 \\times b + a \\\\ |
|
|
|
|
39
|
|
|
&= 100001 \\times a + 10010 \\times b + 110 \\times c \\\\ |
40
|
|
|
&= 11 \\times (9091 \\times a + 910 \\times b + 10 \\times c) |
41
|
|
|
|
42
|
|
|
Since :math:`11 | x \\times y` and :math:`11` is prime then :math:`11 | x` or :math:`11 | y`. Without loss of |
|
|
|
|
43
|
|
|
generality, assume that :math:`11 | y`. |
44
|
|
|
|
45
|
|
|
To solve this problem search through all :math:`3`-digit numbers :math:`x,y \\in \\mathbb{N}` where :math:`11 | y`. |
|
|
|
|
46
|
|
|
Then, identify all palindromic products :math:`x \\times y`. Find the maximum such :math:`x \\times y`. |
|
|
|
|
47
|
|
|
|
48
|
|
|
.. note:: if the maximal product was a :math:`5`-digit number then an alternative approach would be needed. |
|
|
|
|
49
|
|
|
|
50
|
|
|
Solution Implementation |
51
|
|
|
####################### |
52
|
|
|
|
53
|
|
|
.. literalinclude:: ../../solutions/problem4.py |
54
|
|
|
:language: python |
55
|
|
|
:lines: 58- |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
from lib.digital import is_palindrome |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def solve(): |
62
|
|
|
""" Compute the answer to Project Euler's problem #4 """ |
63
|
|
|
answer = 0 |
64
|
|
|
for x in range(100, 1000): # all three digit decimal numbers |
|
|
|
|
65
|
|
|
for y in range(110, 1000, 11): # all three digit decimal numbers that are divisible by 11 |
|
|
|
|
66
|
|
|
if is_palindrome(x * y): |
67
|
|
|
answer = max(answer, x * y) |
68
|
|
|
return answer |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
expected_answer = 906609 |
|
|
|
|
72
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.