sol1.py 911 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number,
134217728=89, is a ninth power.
How many n-digit positive integers exist which are also an nth power?
"""

"""
The maximum base can be 9 because all n-digit numbers < 10^n.
Now 9**23 has 22 digits so the maximum power can be 22.
Using these conclusions, we will calculate the result.
"""


14
def solution(max_base: int = 10, max_power: int = 22) -> int:
15 16
    """
    Returns the count of all n-digit numbers which are nth power
17
    >>> solution(10, 22)
18
    49
19
    >>> solution(0, 0)
20
    0
21
    >>> solution(1, 1)
22
    0
23
    >>> solution(-1, -1)
24 25 26 27 28
    0
    """
    bases = range(1, max_base)
    powers = range(1, max_power)
    return sum(
C
Christian Clauss 已提交
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"{solution(10, 22) = }")