Skip to content

Commit 194b56d

Browse files
Kush1101cclauss
andauthored
Created problem_63 in project_euler (#2357)
* Create __init__.py * Add files via upload * Update project_euler/problem_63/sol1.py Co-authored-by: Christian Clauss <[email protected]> * Update sol1.py * Update sol1.py * Update sol1.py Co-authored-by: Christian Clauss <[email protected]>
1 parent cf385ad commit 194b56d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

project_euler/problem_63/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

project_euler/problem_63/sol1.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number,
3+
134217728=89, is a ninth power.
4+
How many n-digit positive integers exist which are also an nth power?
5+
"""
6+
7+
"""
8+
The maximum base can be 9 because all n-digit numbers < 10^n.
9+
Now 9**23 has 22 digits so the maximum power can be 22.
10+
Using these conclusions, we will calculate the result.
11+
"""
12+
13+
14+
def compute_nums(max_base: int = 10, max_power: int = 22) -> int:
15+
"""
16+
Returns the count of all n-digit numbers which are nth power
17+
>>> compute_nums(10, 22)
18+
49
19+
>>> compute_nums(0, 0)
20+
0
21+
>>> compute_nums(1, 1)
22+
0
23+
>>> compute_nums(-1, -1)
24+
0
25+
"""
26+
bases = range(1, max_base)
27+
powers = range(1, max_power)
28+
return sum(
29+
1 for power in powers for base in bases if len(str((base ** power))) == power
30+
)
31+
32+
33+
if __name__ == "__main__":
34+
print(f"{compute_nums(10, 22) = }")

0 commit comments

Comments
 (0)