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`: |
|
|
|
|
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` |
|
|
|
|
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 |
|
|
|
|
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): |
|
|
|
|
54
|
|
|
terms.add(a ** b) |
55
|
|
|
answer = len(terms) |
56
|
|
|
return answer |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
expected_answer = 9183 |
|
|
|
|
60
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.