1 | """ |
||
2 | Project Euler Problem 29: Distinct Powers |
||
3 | ========================================= |
||
4 | |||
5 | .. module:: solutions.problem29 |
||
6 | :synopsis: My solution to problem #29. |
||
7 | |||
8 | The source code for this problem can be |
||
9 | `found here <https://bitbucket.org/nekedome/project-euler/src/master/solutions/problem29.py>`_. |
||
10 | |||
11 | Problem Statement |
||
12 | ################# |
||
13 | |||
14 | Consider all integer combinations of :math:`a^b` for :math:`2 \\le a \\le 5` and :math:`2 \\le b \\le 5`: |
||
0 ignored issues
–
show
|
|||
15 | |||
16 | .. math:: |
||
17 | |||
18 | &2^2=4, &2^3=8, &2^4=16, &2^5=32 \\\\ |
||
19 | &3^2=9, &3^3=27, &3^4=81, &3^5=243 \\\\ |
||
20 | &4^2=16, &4^3=64, &4^4=256, &4^5=1024 \\\\ |
||
21 | &5^2=25, &5^3=125, &5^4=625, &5^5=3125 \\\\ |
||
22 | |||
23 | If they are then placed in numerical order, with any repeats removed, we get the following sequence of :math:`15` |
||
0 ignored issues
–
show
|
|||
24 | distinct terms: |
||
25 | |||
26 | .. math:: |
||
27 | |||
28 | 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 |
||
29 | |||
30 | How many distinct terms are in the sequence generated by :math:`a^b` for :math:`2 \\le a \\le 100` and |
||
0 ignored issues
–
show
|
|||
31 | :math:`2 \\le b \\le 100`? |
||
32 | |||
33 | Solution Discussion |
||
34 | ################### |
||
35 | |||
36 | Nothing sophisticated here, explicitly perform the computation. |
||
37 | |||
38 | Solution Implementation |
||
39 | ####################### |
||
40 | |||
41 | .. literalinclude:: ../../solutions/problem29.py |
||
42 | :language: python |
||
43 | :lines: 46- |
||
44 | """ |
||
45 | |||
46 | from itertools import product |
||
47 | |||
48 | |||
49 | def solve(): |
||
50 | """ Compute the answer to Project Euler's problem #29 """ |
||
51 | upper_bound = 100 |
||
52 | terms = set() |
||
53 | for a, b in product(range(2, upper_bound + 1), repeat=2): |
||
0 ignored issues
–
show
The name
a 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. ![]() The name
b 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. ![]() |
|||
54 | terms.add(a ** b) |
||
55 | answer = len(terms) |
||
56 | return answer |
||
57 | |||
58 | |||
59 | expected_answer = 9183 |
||
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. ![]() |
|||
60 |
This check looks for lines that are too long. You can specify the maximum line length.