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 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
import sys, os; sys.path.append(os.path.abspath('..'))
from timer import timethis
for p in range(10, 50):
if len(str(pow(9, p))) < p:
print(p)
break
@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
solve()