Check whether N is Kth power of an integer
Given two numbers N and K. The task is to check whether N is Kth power to any integer i.e., whether N can be expressed as XK, where X is an integer.
Examples:
Input: N = 81, K = 4
Output: True
Explanation: 81 can be expressed as 34
Input: N = 26, K = 2
Output: False
Explanation: 26 can not be expressed as power of 2 to any number
Input: N = 512, K = 3
Output: True
Explanation: 512 can be expressed 83
Naive Approach: To solve this problem we can traverse from 1 to N and check whether the number N is Kth power to the current number.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The efficient approach to solve the above problem is based on the following idea:
Say the number N is Kth power of some integer X. So,
N = XK
or X = N1/K. Now if X is an integer then the solution exists.
So we need to find if the floor of Kth root of N is the actual Kth root or not
Follow the steps mentioned below to implement the idea:
- Check if K is 0 or not. If K is 0 and N = 1 then its possible that N is Kth power of any number.
- Store the reciprocal of K (say x) and then find the xth root of N.
- If both the ceil and the floor value of xth root of N is equal then it is possible that N is Kth of any number.
- If none of the above conditions is true then no such solution is possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_Kth_power( int n, int k)
{
if (k == 0) {
if (n == 1) {
return true ;
}
return false ;
}
else {
double reciprocal = ( double )(1) / ( double )(k);
double result = pow (n, reciprocal);
if ( floor (result) == ceil (result)) {
return true ;
}
return false ;
}
}
int main()
{
int N = 81;
int K = 4;
bool ans = check_Kth_power(N, K);
if (ans)
cout << "True" ;
else
cout << "False" ;
return 0;
}
|
C
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool check_Kth_power( int n, int k)
{
if (k == 0) {
if (n == 1) {
return true ;
}
return false ;
}
else {
double reciprocal = ( double )(1) / ( double )(k);
double result = pow (n, reciprocal);
if ( floor (result) == ceil (result)) {
return true ;
}
return false ;
}
}
int main()
{
int N = 81;
int K = 4;
bool ans = check_Kth_power(N, K);
if (ans == true )
printf ( "True" );
else
printf ( "False" );
return 0;
}
|
Java
import java.io.*;
import java.lang.Math;
class GFG {
public static Boolean check_Kth_power( int n,
int k)
{
if (k == 0 ) {
if (n == 1 ) {
return true ;
}
return false ;
}
else {
double reciprocal = ( double )( 1 ) / ( double )(k);
double result = Math.pow(n, reciprocal);
if (Math.floor(result) == Math.ceil(result)) {
return true ;
}
return false ;
}
}
public static void main(String[] args)
{
int N = 81 ;
int K = 4 ;
Boolean ans = check_Kth_power(N, K);
if (ans == true )
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
import math
def check_Kth_power( n, k):
if (k = = 0 ):
if (n = = 1 ):
return True
return False
else :
reciprocal = ( 1 ) / / (k)
result = n * * reciprocal
if math.floor(result) = = math.ceil(result):
return True
return False
N = 81
K = 4
ans = check_Kth_power(N, K)
if (ans):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
public class GFG {
static Boolean check_Kth_power( int n, int k)
{
if (k == 0) {
if (n == 1) {
return true ;
}
return false ;
}
else {
double reciprocal = ( double )(1) / ( double )(k);
double result = Math.Pow(n, reciprocal);
if (Math.Floor(result)
== Math.Ceiling(result)) {
return true ;
}
return false ;
}
}
static public void Main()
{
int N = 81;
int K = 4;
Boolean ans = check_Kth_power(N, K);
if (ans == true )
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
Javascript
<script>
function check_Kth_power(n, k)
{
if (k == 0) {
if (n == 1) {
return true ;
}
return false ;
}
else {
let reciprocal = 1 / k;
let result = Math.pow(n, reciprocal);
if (Math.floor(result) == Math.ceil(result)) {
return true ;
}
return false ;
}
}
let N = 81;
let K = 4;
let ans = check_Kth_power(N, K);
if (ans)
console.log( "True" );
else
console.log( "False" );
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...