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++
// 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
filter_none
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"); } } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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) |
chevron_right
filter_none
Output:
Yes
Time Complexity : O(sqrt(n))
Recommended Posts:
- Check if a number can be expressed as 2^x + 2^y
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number can be expressed as sum two abundant numbers
- Check if a number can be expressed as a sum of consecutive numbers
- 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 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
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number is divisible by all prime divisors of another number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Program to check whether a number is Proth number or not
- Check if a number with even number of digits is palindrome or not
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.



