Count perfect power of K in a range [L, R]
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.
Examples:
Input: L = 3, R = 16, K = 3
Output: 1
Explanation:
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
Output: 2
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
double nthRoot( int A, int N)
{
double xPre = rand () % 10;
double eps = 1e-3;
double delX = INT_MAX;
double xK;
while (delX > eps) {
xK = ((N - 1.0) * xPre +
( double )A / pow (xPre, N - 1))
/ ( double )N;
delX = abs (xK - xPre);
xPre = xK;
}
return xK;
}
int countPowers( int a, int b, int k)
{
return ( floor (nthRoot(b, k)) -
ceil (nthRoot(a, k)) + 1);
}
int main()
{
int a = 7, b = 28, k = 2;
cout << "Count of Powers is "
<< countPowers(a, b, k);
return 0;
}
|
Java
class GFG{
static double nthRoot( int A, int N)
{
double xPre = Math.random()* 10 % 10 ;
double eps = 1e- 3 ;
double delX = Integer.MAX_VALUE;
double xK = 0 ;
while (delX > eps)
{
xK = ((N - 1.0 ) * xPre +
( double )A / Math.pow(xPre, N - 1 ))
/ ( double )N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
static int countPowers( int a, int b, int k)
{
return ( int ) (Math.floor(nthRoot(b, k)) -
Math.ceil(nthRoot(a, k)) + 1 );
}
public static void main(String[] args)
{
int a = 7 , b = 28 , k = 2 ;
System.out.print( "Count of Powers is "
+ countPowers(a, b, k));
}
}
|
Python3
import sys
from math import pow ,ceil,floor
import random
def nthRoot(A,N):
xPre = (random.randint( 0 , 9 )) % 10
eps = 1e - 3
delX = sys.maxsize
while (delX > eps):
xK = ((N - 1.0 ) * xPre + A / pow (xPre, N - 1 )) / N
delX = abs (xK - xPre)
xPre = xK
return xK
def countPowers(a, b, k):
return (floor(nthRoot(b, k)) - ceil(nthRoot(a, k)) + 1 )
if __name__ = = '__main__' :
a = 7
b = 28
k = 2
print ( "Count of Powers is" ,countPowers(a, b, k))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static double nthRoot( int A, int N)
{
Random r = new Random();
double xPre = r.Next(0,9);
double eps = 1e-3;
double delX = int .MaxValue;
double xK = 0;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
( double )A / Math.Pow(xPre, N - 1))
/ ( double )N;
delX = Math.Abs(xK - xPre);
xPre = xK;
}
return xK;
}
static int countPowers( int a, int b, int k)
{
return ( int ) (Math.Floor(nthRoot(b, k)) -
Math.Ceiling(nthRoot(a, k)) + 1);
}
public static void Main(String[] args)
{
int a = 7, b = 28, k = 2;
Console.Write( "Count of Powers is "
+ countPowers(a, b, k));
}
}
|
Javascript
<script>
function nthRoot(A, N)
{
var xPre = Math.random() % 10;
var eps = 0.001;
var delX = 1000000000;
var xK;
while (delX > eps) {
xK = ((N - 1.0) * xPre +
A / Math.pow(xPre, N - 1))
/ N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
function countPowers(a, b, k)
{
return (Math.floor(nthRoot(b, k)) -
Math.ceil(nthRoot(a, k)) + 1);
}
var a = 7, b = 28, k = 2;
document.write( "Count of Powers is "
+ countPowers(a, b, k));
</script>
|
Output
Count of Powers is 3
Performance Analysis:
- 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).
Last Updated :
27 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...