Project Euler 63: Powerful digit counts
Problem statement
How many -digit positive integers exist that are also an power?
ex => 16807 is a 5-digit number and also 7^5.
Solution approach
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.
-
leastis a number whose power is the smallestn-digit power of any number. -
largestis a number whose power is the largestn-digit power of any number.
Example
- For
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)