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 |
||
0 ignored issues
–
show
|
|||
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, |
||
0 ignored issues
–
show
|
|||
25 | if such numbers were palindromes, they could be expressed in base :math:`10` in one of the following two ways: |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | .. math:: |
||
28 | |||
29 | abcba &= & 10000 \\times a &+ 1000 \\times b &+ 100 \\times c &+ 10 \\times b &+ a \\\\ |
||
0 ignored issues
–
show
|
|||
30 | abccba &= 100000 \\times a +& 10000 \\times b &+ 1000 \\times c &+ 100 \\times c &+ 10 \\times b &+ a \\\\ |
||
0 ignored issues
–
show
|
|||
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: |
||
0 ignored issues
–
show
|
|||
35 | |||
36 | .. math:: |
||
37 | |||
38 | abccba &= 100000 \\times a + 10000 \\times b + 1000 \\times c + 100 \\times c + 10 \\times b + a \\\\ |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
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`. |
||
0 ignored issues
–
show
|
|||
46 | Then, identify all palindromic products :math:`x \\times y`. Find the maximum such :math:`x \\times y`. |
||
0 ignored issues
–
show
|
|||
47 | |||
48 | .. note:: if the maximal product was a :math:`5`-digit number then an alternative approach would be needed. |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
The name
x does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ ).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. ![]() |
|||
65 | for y in range(110, 1000, 11): # all three digit decimal numbers that are divisible by 11 |
||
0 ignored issues
–
show
The name
y does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$ ).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. ![]() |
|||
66 | if is_palindrome(x * y): |
||
67 | answer = max(answer, x * y) |
||
68 | return answer |
||
69 | |||
70 | |||
71 | expected_answer = 906609 |
||
0 ignored issues
–
show
The name
expected_answer does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$ ).
This check looks for invalid names for a range of different identifiers. You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements. If your project includes a Pylint configuration file, the settings contained in that file take precedence. To find out more about Pylint, please refer to their site. ![]() |
|||
72 |
This check looks for lines that are too long. You can specify the maximum line length.