Powerful digit counts

The 5-digit number, $16807=7^5$, is also a fifth power. Similarly, the 9-digit number, $134217728=8^9$, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

Solution note:

The base may not be larger than 9, as $10^n$ has $n+1$ digits.

Observation: If $a^n$ does not have $n$ digits, then neither does $a^m$ for any $m>n$.


In [12]:
from itertools import count
s = 0
for base in range(1, 10):
    for exp in count(1):
        if len(str(base**exp)) == exp:
            s += 1
            print(base, exp, base**exp)
        else: break
print(s)


1 1 1
2 1 2
3 1 3
4 1 4
4 2 16
5 1 5
5 2 25
5 3 125
6 1 6
6 2 36
6 3 216
6 4 1296
7 1 7
7 2 49
7 3 343
7 4 2401
7 5 16807
7 6 117649
8 1 8
8 2 64
8 3 512
8 4 4096
8 5 32768
8 6 262144
8 7 2097152
8 8 16777216
8 9 134217728
8 10 1073741824
9 1 9
9 2 81
9 3 729
9 4 6561
9 5 59049
9 6 531441
9 7 4782969
9 8 43046721
9 9 387420489
9 10 3486784401
9 11 31381059609
9 12 282429536481
9 13 2541865828329
9 14 22876792454961
9 15 205891132094649
9 16 1853020188851841
9 17 16677181699666569
9 18 150094635296999121
9 19 1350851717672992089
9 20 12157665459056928801
9 21 109418989131512359209
49

In [ ]: