Given three integers L, R, and K, the task is to find the count of the number of integers between L to R, which are perfect power of K. That is the number of integers that are in the form of aK, where a can be any number.
Input: L = 3, R = 16, K = 3
There is only one integer between 3 to 16 which is in the for aK which is 8.
Input: L = 7, R = 18, K = 2
There are two such numbers that are in the form of aK and are in the range of 7 to 18 which is 9 and 16.
Approach: The idea is to find the Kth root of the L and R respectively, where Kth root of a number N is a real number that gives N, when we raise it to integer power N. Then the count of integers which are the power of K in the range L and R can be defined as –
Count = ( floor(Kthroot(R)) - ceil(Kthroot(L)) + 1 )
Kth root of a number N can be calculated using Newton’s Formulae, where ith iteration can be calculated using the below formulae –
x(i + 1) = (1 / K) * ((K – 1) * x(i) + N / x(i) ^ (N – 1))
Below is the implementation of the above approach:
- Time Complexity: As in the above approach, there are two function calls for finding Nth root of the number which takes O(logN) time, Hence the Time Complexity will be O(logN).
- Space Complexity: As in the above approach, there is no extra space used, Hence the space complexity will be O(1).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count numbers upto N which are both perfect square and perfect cube
- Find the sum of power of bit count raised to the power B
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Perfect power (1, 4, 8, 9, 16, 25, 27, ...)
- Smallest N digit number which is a perfect fourth power
- Calculate sum of all integers from 1 to N, excluding perfect power of 2
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Count of primes in a given range that can be expressed as sum of perfect squares
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Count of elements which are power of 2 in a given range subarray for Q queries
- Perfect cubes in a range
- Print all perfect squares from the given range
- Sum of all Perfect Squares lying in the range [L, R] for Q queries
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Sum of all Perfect numbers lying in the range [L, R]
- Probability of getting a perfect square when a random number is chosen in a given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.