Powerful digit counts

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?


Idea

The key to this problem is to decide the upper bound of $ n $.

And it is easy to notice that $ 1 \le i \le 9 $, otherwise $ i^n $ will exceed $ n $ digits


In [1]:
import sys, os; sys.path.append(os.path.abspath('..'))
from timer import timethis
In [2]:
for p in range(10, 50):
    if len(str(pow(9, p))) < p:
        print(p)
        break
22
In [3]:
@timethis
def solve():
    cnt = 0
    for i in range(1, 22):
        cnt += sum(map(lambda n: len(n) == i, [str(pow(j, i)) for j in range(1, 10)]))
    return cnt
In [4]:
solve()
Run for 0.000 seconds
Out[4]:
49