Open In App

Check whether N is Kth power of an integer

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether n is
// kth power of an integer
bool check_Kth_power(int n, int k)
{
    // Checking for special case when k=0
    if (k == 0) {
        if (n == 1) {
            return true;
        }
        return false;
    }
    else {
 
        // Calculating reciprocal of k
        double reciprocal = (double)(1) / (double)(k);
 
        // The result for n^(1/k)
        double result = pow(n, reciprocal);
 
        // Checking condition for an integer
        if (floor(result) == ceil(result)) {
            return true;
        }
        return false;
    }
}
 
// Driver Code
int main()
{
    int N = 81;
    int K = 4;
 
    // Function call
    bool ans = check_Kth_power(N, K);
    if (ans)
        cout << "True";
    else
        cout << "False";
    return 0;
}


C




// C code to implement the approach
 
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
 
// Function to check  whether n is kth power
// to an integer
bool check_Kth_power(int n, int k)
{
    // Checking for special case when k=0
    if (k == 0) {
        if (n == 1) {
            return true;
        }
        return false;
    }
 
    else {
 
        // Calculating reciprocal of k
        double reciprocal = (double)(1) / (double)(k);
 
        // The result for n^(1/k)
        double result = pow(n, reciprocal);
 
        // Checking condition for an integer
        if (floor(result) == ceil(result)) {
            return true;
        }
        return false;
    }
}
 
// Driver code
int main()
{
    int N = 81;
    int K = 4;
 
    // Function call
    bool ans = check_Kth_power(N, K);
    if (ans == true)
        printf("True");
    else
        printf("False");
    return 0;
}


Java




// Java code  to implement the approach
 
import java.io.*;
import java.lang.Math;
 
class GFG {
 
    // Function to check  whether n is kth power
    // to an integer
    public static Boolean check_Kth_power(int n,
                                          int k)
    {
        // Checking for special case when k=0
        if (k == 0) {
            if (n == 1) {
                return true;
            }
            return false;
        }
        else {
 
            // Calculating reciprocal of k
            double reciprocal = (double)(1) / (double)(k);
 
            // The result for n^(1/k)
            double result = Math.pow(n, reciprocal);
 
            // Checking condition for an integer
            if (Math.floor(result) == Math.ceil(result)) {
                return true;
            }
            return false;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 81;
        int K = 4;
 
        // Function call
        Boolean ans = check_Kth_power(N, K);
        if (ans == true)
            System.out.println("True");
        else
            System.out.println("False");
    }
}


Python3




# python code to implement the approach
import math
 
# Function to check whether n is
# kth power of an integer
def check_Kth_power( n, k):
   
  # Checking for special case when k=0
  if (k == 0):
    if (n == 1):
      return True
    return False
  else:
    # Calculating reciprocal of k
    reciprocal = (1) // (k)
     
    # The result for n^(1/k)
    result = n**reciprocal
     
    # Checking condition for an integer
    if math.floor(result) == math.ceil(result):
      return True
         
    return False
     
# Driver Code
N = 81
K = 4
 
# Function call
ans = check_Kth_power(N, K)
if (ans):
  print("True")
else:
  print("False")
   
  # This code is contributed by rohitsingh07052.


C#




// C# code to implement the approach
using System;
 
public class GFG {
 
    // Function to check  whether n is kth power
    // to an integer
    static Boolean check_Kth_power(int n, int k)
    {
        // Checking for special case when k=0
        if (k == 0) {
            if (n == 1) {
                return true;
            }
            return false;
        }
        else {
 
            // Calculating reciprocal of k
            double reciprocal = (double)(1) / (double)(k);
 
            // The result for n^(1/k)
            double result = Math.Pow(n, reciprocal);
 
            // Checking condition for an integer
            if (Math.Floor(result)
                == Math.Ceiling(result)) {
                return true;
            }
            return false;
        }
    }
 
    // Driver Code
    static public void Main()
    {
        int N = 81;
        int K = 4;
 
        // Function call
        Boolean ans = check_Kth_power(N, K);
        if (ans == true)
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}
 
// This code is contributed by Rohit Pradhan


Javascript




<script>
 
// Function to check whether n is
// kth power of an integer
function check_Kth_power(n, k)
{
    // Checking for special case when k=0
    if (k == 0) {
        if (n == 1) {
            return true;
        }
        return false;
    }
    else {
 
        // Calculating reciprocal of k
        let reciprocal = 1 / k;
 
        // The result for n^(1/k)
        let result = Math.pow(n, reciprocal);
 
        // Checking condition for an integer
        if (Math.floor(result) == Math.ceil(result)) {
            return true;
        }
        return false;
    }
}
 
// Driver Code
 
let N = 81;
let K = 4;
 
// Function call
let ans = check_Kth_power(N, K);
 
if (ans)
    console.log("True");
else
    console.log("False");
 
// This code is contributed by shinjanpatra
 
</script>


Output

True

Time Complexity: O(log N)
Auxiliary Space: O(1)



Last Updated : 09 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads