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++
// 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; } |
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
<?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) |
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.
Recommended Posts:
- Check if a number can be expressed as 2^x + 2^y
- Check if a number N can be expressed in base B
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as sum two abundant numbers
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as a sum of consecutive numbers
- Check if a number can be expressed as a product of exactly K prime divisors
- Check whether a number can be expressed as a product of single digit numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if N can be expressed as product of 3 distinct numbers
- Check if an integer can be expressed as a sum of two semi-primes
- Number expressed as sum of five consecutive integers
- N expressed as sum of 4 prime numbers
- Elements of Array which can be expressed as power of prime numbers
- Elements of Array which can be expressed as power of some integer to given exponent K
- Count of primes in a given range that can be expressed as sum of perfect squares
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check if a large number is divisible by a number which is a power of 2
- Check if a number is divisible by all prime divisors of another number
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.

