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

11 May, 2021
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
22 = 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.
Consider a no. N which needs 
to be expressed in the form (a^b).
N = ab
Taking log both sides:
log (N) = b.log (a)
b = log(N)/log(a)
Keep this logic in mind to develop the most efficient solution mentioned below:
C++
Java
Python 3
C#
PHP
Javascript
// 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 codeint main(){    int n = 16;    cout << (isPower(n) ? "Yes" : "No");    return 0;}
//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");     }}
# 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 Codeif __name__ == "__main__" :     n = 16     if isPower(n) :        print("Yes")    else :        print("No") # This code is contributed by ANKITRAI1
// 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 codepublic static void Main(string[] args){    int n = 16;    Console.WriteLine(isPower(n) ?                           "Yes" : "No");}} // This code is contributed// by Shrikant13
<?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)
<script>//Javascript program to check if a number//can be expressed as a^b.         function isPower(a)    {        if (a == 1)        return true;      for (let i = 2; i * i <= a; i++) {        let val = Math.log(a) / Math.log(i);        if ((val - Math.floor(val)) < 0.00000001)            return true;         }      return false;         }         // Driver code    let n = 16;    document.write(isPower(n) ? "Yes" : "No");              // This code is contributed by avanitrachhadiya2155</script>
Yes
Time Complexity : O(sqrt(n))
ankthon
ukasp
shrikanth13
Akanksha_Rai
newtocoding
DeepBoro
avanitrachhadiya2155
maths-log
number-theory
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.