How many -digit positive integers exist that are also an power?
ex => 16807 is a 5-digit number and also 7^5.
Any n
-digit number x
can be represented as follows:
10^(n-1) < x < 10^n
We will run a while
loop for n
from 1, and increment the result by largest
-least
until least
> 10.
least
is a number whose power is the smallest n
-digit power of any number.
largest
is a number whose power is the largest n
-digit power of any number.
n
=2:
least
= 4 because is 9, which is 1-digit.
largest
= 9 because is 100, which is 3-digit.
So, we will increment the result by largest
-least
+1, i.e., 9-4+1=6.
from math import ceil# ceil(x) returns least whole number greater than xresult=0least=1largest=9n=1while least<10 :least = ceil((10**(n-1))**(1/n))result += largest-least +1# +1 because range of (least,largest) is inclusiven+=1print(result)