Check if a number can be expressed as a^b | Set 2

You have given a number n. Check if a number can be represented in the form of pow(a, b) (a^b).

Examples:

Input : 4
Output : Yes
2^2 = 4

Input : 12
Output : No

We have discussed two approaches in Check if a number can be expressed as x^y (x raised to power y). In this post, a more efficient solution is discussed. The idea is based on logarithmic.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if a number
// can be expressed as a^b.
#include <bits/stdc++.h>
using namespace std;
  
bool isPower(int a)
{
    if (a == 1)
        return true;
  
    for (int i = 2; i * i <= a; i++) {
        double val = log(a) / log(i);
        if ((val - (int)val) < 0.00000001)
            return true;
    }
  
    return false;
}
  
// Driver code
int main()
{
    int n = 16;
    cout << (isPower(n) ? "Yes" : "No");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to check if a number
//can be expressed as a^b.
  
public class GFG {
  
    static boolean isPower(int a)
    {
    if (a == 1)
        return true;
  
    for (int i = 2; i * i <= a; i++) {
        double val = Math.log(a) / Math.log(i);
        if ((val - (int)val) < 0.00000001)
            return true;
    }
  
    return false;
    }
      
    // Driver code
    public static void main(String[] args) {
          
        int n = 16;
        System.out.println(isPower(n) ? "Yes" : "No");
  
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to check if a number 
# can be expressed as a^b
  
from math import *
  
def isPower(a) :
    if a== 1 :
        return True
  
    for i in range(2, int(sqrt(a)) + 1) :
        val = log(a) / log(i)
  
        if (round((val - int(val)),8) < 0.00000001):
            return True
      
    return False
  
  
# Driver Code
if __name__ == "__main__" :
  
    n = 16
  
    if isPower(n) :
        print("Yes")
    else :
        print("No")
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a number 
// can be expressed as a^b. 
using System;
  
class GFG
{
  
public static bool isPower(int a)
{
    if (a == 1)
    {
        return true;
    }
  
    for (int i = 2; i * i <= a; i++)
    {
        double val = Math.Log(a) / 
                     Math.Log(i);
        if ((val - (int)val) < 0.00000001)
        {
            return true;
        }
    }
return false;
}
  
// Driver code 
public static void Main(string[] args)
{
    int n = 16;
    Console.WriteLine(isPower(n) ? 
                           "Yes" : "No");
}
}
  
// This code is contributed 
// by Shrikant13

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if a number
// can be expressed as a^b.
  
function isPower($a)
{
    if ($a == 1)
        return true;
  
    for ($i = 2; $i * $i <= $a; $i++)
    {
        $val = log($a) / log($i);
        if (($val - $val) < 0.00000001)
            return true;
    }
  
    return false;
}
  
// Driver code
$n = 16;
echo (isPower($n) ? "Yes" : "No");
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Output:

Yes

Time Complexity : O(sqrt(n))



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.