-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe29.py
More file actions
75 lines (58 loc) · 2.13 KB
/
pe29.py
File metadata and controls
75 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Problem 29
Consider all integer combinations of a**b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
If they are then placed in numerical order, with any repeats removed,
we get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by a**b for 2 ≤ a ≤ 100
and 2 ≤ b ≤ 100?
Ans: 9183
'''
import numpy as np
from typing import Tuple
Range = Tuple[int, int] # List[int, int] is unsupported however for Python 3.9+ we can do list[int, int]
def find_combos(bases: np.array, powers: np.array) -> set:
'''
QA investigation found that an input of findCombos(np.arange(2,10+1), np.arange(2,10+1))
returns a set containing -808182895 which is an error.
'''
combos = set([])
for i in bases:
combos.update(np.power(i, powers))
return combos
def generate_combinations(a_range: Range, b_range: Range) -> set:
'''
Parameters
----------
a_range : Range
The min and max (inclusive) values of bases to consider.
b_range : Range
The min and max (inclusive) values of powers to consider.
Raises
------
TypeError
If input is not a valid Tuple[int, int].
ValueError
If input contains value outside of allowed bounds of [2, 100].
Returns
-------
set
Set of distinct terms with bases a and powers b corresponding
to the equation a**b.
'''
if sum(type(element)!=int for element in a_range + b_range):
raise TypeError("Function only accepts lists of two integers as inputs.")
elif sum((element > 100 or element < 2) for element in a_range + b_range):
raise ValueError("Input contains value outside of allowed bounds of [2, 100].")
combos = set()
a1, a2 = a_range
b1, b2 = b_range
for a in range(a1, a2+1):
for b in range(b1, b2+1):
combos.update([a**b])
return combos
def number_of_distinct_terms(combos: set) -> int:
return len(list(combos))
if __name__ == "__main__":
result = generate_combinations([2, 100], [2, 100])
print(number_of_distinct_terms(result))