1 | """ |
||
2 | Project Euler Problem 44: Pentagon Numbers |
||
3 | ========================================== |
||
4 | |||
5 | .. module:: solutions.problem44 |
||
6 | :synopsis: My solution to problem #44. |
||
7 | |||
8 | The source code for this problem can be |
||
9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem44.py>`_. |
||
10 | |||
11 | Problem Statement |
||
12 | ################# |
||
13 | |||
14 | Pentagonal numbers are generated by the formula, :math:`P_n = \\frac{n(3n-1)}{2}`. The first ten pentagonal numbers are: |
||
0 ignored issues
–
show
|
|||
15 | |||
16 | .. math:: |
||
17 | |||
18 | 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, \\dots |
||
19 | |||
20 | It can be seen that :math:`P_4 + P_7 = 22 + 70 = 92 = P_8`. However, their difference, :math:`70 - 22 = 48`, is not |
||
0 ignored issues
–
show
|
|||
21 | pentagonal. |
||
22 | |||
23 | Find the pair of pentagonal numbers, :math:`P_j` and :math:`P_k`, for which their sum and difference are pentagonal and |
||
0 ignored issues
–
show
|
|||
24 | :math:`D = |P_k - Pj|` is minimised; what is the value of :math:`D`? |
||
25 | |||
26 | Solution Discussion |
||
27 | ################### |
||
28 | |||
29 | This solution is really a brute-force search with some sensible ordering constraints to remove redundant work that would |
||
0 ignored issues
–
show
|
|||
30 | otherwise occur. |
||
31 | |||
32 | Iterate through a two-dimensional collection of pentagonal numbers, :math:`\\lbrace (p_j, p_k) \\rbrace`, and simply |
||
0 ignored issues
–
show
|
|||
33 | test whether both the numbers :math:`p_j + p_k` and :math:`|p_j - p_k|` are pentagonal. |
||
34 | |||
35 | By iterating over :math:`p_k` first, and in ascending order, and then :math:`p_j` second in descending order from |
||
0 ignored issues
–
show
|
|||
36 | :math:`p_{k-1}` to :math:`p_1` we will guarantee that :math:`p_k \\gt p_j`. Therefore, we will only consider positive |
||
0 ignored issues
–
show
|
|||
37 | values for :math:`p_k + p_j` and :math:`p_k - p_j`. |
||
38 | |||
39 | Further, by initialising :math:`p_j` to a value as close to :math:`p_k` as possible and then increasing the distance |
||
0 ignored issues
–
show
|
|||
40 | between :math:`p_j,p_k` at each iteration, the first candidate :math:`(p_j, p_k)` satisfying the search conditions will |
||
0 ignored issues
–
show
|
|||
41 | have minimal distance :math:`|p_j - p_k|` for the current value of :math:`p_k`. |
||
42 | |||
43 | Solution Implementation |
||
44 | ####################### |
||
45 | |||
46 | .. literalinclude:: ../../solutions/problem44.py |
||
47 | :language: python |
||
48 | :lines: 51- |
||
49 | """ |
||
50 | |||
51 | from lib.sequence import Pentagonals |
||
52 | |||
53 | |||
54 | def solve(): |
||
55 | """ Compute the answer to Project Euler's problem #44 """ |
||
56 | pentagonals = Pentagonals() |
||
57 | visited_pentagonals = [] # will reverse-iterate over this list {p_j} |
||
58 | pentagonals_set = set() # for testing whether sums or differences of pentagonal numbers are pentagonal numbers |
||
0 ignored issues
–
show
|
|||
59 | for p_k in pentagonals: |
||
60 | for p_j in reversed(visited_pentagonals): |
||
61 | if (p_k - p_j) in pentagonals_set and (p_k + p_j) in pentagonals: |
||
0 ignored issues
–
show
|
|||
62 | return p_k - p_j # p_k > p_j => |p_k - p_j| = p_k = p_j |
||
63 | visited_pentagonals.append(p_k) |
||
64 | pentagonals_set.add(p_k) |
||
65 | |||
66 | |||
67 | expected_answer = 5482660 |
||
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. ![]() |
|||
68 |
This check looks for lines that are too long. You can specify the maximum line length.