1 | """ |
||
2 | Project Euler Problem 8: Largest Product In A Series |
||
3 | ==================================================== |
||
4 | |||
5 | .. module:: solutions.problem8 |
||
6 | :synopsis: My solution to problem #8. |
||
7 | |||
8 | The source code for this problem can be |
||
9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem8.py>`_. |
||
10 | |||
11 | Problem Statement |
||
12 | ################# |
||
13 | |||
14 | The four adjacent digits in the :math:`1000`-digit number that have the greatest product are |
||
15 | :math:`\\color{red}{9} \\times \\color{red}{9} \\times \\color{red}{8} \\times \\color{red}{9} = 5832`. |
||
0 ignored issues
–
show
|
|||
16 | |||
17 | .. math:: |
||
18 | |||
19 | & 73167176531330624919225119674426574742355349194934 \\hookleftarrow \\\\ |
||
20 | \\hookrightarrow & 96983520312774506326239578318016984801869478851843 \\hookleftarrow \\\\ |
||
21 | \\hookrightarrow & 85861560789112949495459501737958331952853208805511 \\hookleftarrow \\\\ |
||
22 | \\hookrightarrow & 12540698747158523863050715693290963295227443043557 \\hookleftarrow \\\\ |
||
23 | \\hookrightarrow & 66896648950445244523161731856403098711121722383113 \\hookleftarrow \\\\ |
||
24 | \\hookrightarrow & 62229893423380308135336276614282806444486645238749 \\hookleftarrow \\\\ |
||
25 | \\hookrightarrow & 30358907296290491560440772390713810515859307960866 \\hookleftarrow \\\\ |
||
26 | \\hookrightarrow & 70172427121883998797908792274921901699720888093776 \\hookleftarrow \\\\ |
||
27 | \\hookrightarrow & 65727333001053367881220235421809751254540594752243 \\hookleftarrow \\\\ |
||
28 | \\hookrightarrow & 52584907711670556013604839586446706324415722155397 \\hookleftarrow \\\\ |
||
29 | \\hookrightarrow & 53697817977846174064955149290862569321978468622482 \\hookleftarrow \\\\ |
||
30 | \\hookrightarrow & 83972241375657056057490261407972968652414535100474 \\hookleftarrow \\\\ |
||
31 | \\hookrightarrow & 821663704844031\\color{red}{9989}0008895243450658541227588666881 \\hookleftarrow \\\\ |
||
0 ignored issues
–
show
|
|||
32 | \\hookrightarrow & 16427171479924442928230863465674813919123162824586 \\hookleftarrow \\\\ |
||
33 | \\hookrightarrow & 17866458359124566529476545682848912883142607690042 \\hookleftarrow \\\\ |
||
34 | \\hookrightarrow & 24219022671055626321111109370544217506941658960408 \\hookleftarrow \\\\ |
||
35 | \\hookrightarrow & 07198403850962455444362981230987879927244284909188 \\hookleftarrow \\\\ |
||
36 | \\hookrightarrow & 84580156166097919133875499200524063689912560717606 \\hookleftarrow \\\\ |
||
37 | \\hookrightarrow & 05886116467109405077541002256983155200055935729725 \\hookleftarrow \\\\ |
||
38 | \\hookrightarrow & 71636269561882670428252483600823257530420752963450 |
||
39 | |||
40 | Find the thirteen adjacent digits in the :math:`1000`-digit number that have the greatest product. What is the value of |
||
0 ignored issues
–
show
|
|||
41 | this product? |
||
42 | |||
43 | Solution Discussion |
||
44 | ################### |
||
45 | |||
46 | We'll simply iterate over all :math:`13`-long sub-strings in a sliding window fashion. For each sub-string, compute the |
||
0 ignored issues
–
show
|
|||
47 | product of the integers. The maximum of these individuals products is the answer. |
||
48 | |||
49 | Solution Implementation |
||
50 | ####################### |
||
51 | |||
52 | .. literalinclude:: ../../solutions/problem8.py |
||
53 | :language: python |
||
54 | :lines: 57- |
||
55 | """ |
||
56 | |||
57 | from functools import reduce |
||
58 | from operator import mul |
||
59 | |||
60 | |||
61 | def solve(): |
||
62 | """ Compute the answer to Project Euler's problem #8 """ |
||
63 | |||
64 | # Build a list of the individual digits as integer objects |
||
65 | series = """ |
||
66 | 73167176531330624919225119674426574742355349194934 |
||
67 | 96983520312774506326239578318016984801869478851843 |
||
68 | 85861560789112949495459501737958331952853208805511 |
||
69 | 12540698747158523863050715693290963295227443043557 |
||
70 | 66896648950445244523161731856403098711121722383113 |
||
71 | 62229893423380308135336276614282806444486645238749 |
||
72 | 30358907296290491560440772390713810515859307960866 |
||
73 | 70172427121883998797908792274921901699720888093776 |
||
74 | 65727333001053367881220235421809751254540594752243 |
||
75 | 52584907711670556013604839586446706324415722155397 |
||
76 | 53697817977846174064955149290862569321978468622482 |
||
77 | 83972241375657056057490261407972968652414535100474 |
||
78 | 82166370484403199890008895243450658541227588666881 |
||
79 | 16427171479924442928230863465674813919123162824586 |
||
80 | 17866458359124566529476545682848912883142607690042 |
||
81 | 24219022671055626321111109370544217506941658960408 |
||
82 | 07198403850962455444362981230987879927244284909188 |
||
83 | 84580156166097919133875499200524063689912560717606 |
||
84 | 05886116467109405077541002256983155200055935729725 |
||
85 | 71636269561882670428252483600823257530420752963450 |
||
86 | """ |
||
87 | series = series.replace(" ", "").replace("\n", "") |
||
88 | integers = [int(character) for character in series] |
||
89 | |||
90 | # Perform the search through all overlapping m-long subsets |
||
91 | n = len(integers) |
||
0 ignored issues
–
show
The name
n 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. ![]() |
|||
92 | m = 13 |
||
0 ignored issues
–
show
The name
m 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. ![]() |
|||
93 | answer = 0 |
||
94 | for i in range(n - m + 1): |
||
95 | subset = integers[i:i+m] |
||
96 | product = reduce(mul, subset, 1) |
||
97 | answer = max(answer, product) |
||
98 | return answer |
||
99 | |||
100 | |||
101 | expected_answer = 23514624000 |
||
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. ![]() |
|||
102 |
This check looks for lines that are too long. You can specify the maximum line length.