# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// CPP program to check if a number ` `// can be expressed as a^b. ` `#include ` `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; ` `} `

## Java

 `//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

 `# 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 `

## C#

 `// 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 `

## PHP

 `

Output:

```Yes
```

Time Complexity : O(sqrt(n))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Article Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.