Given two positive numbers x and y, check if y is a power of x or not.
Examples :
Input: x = 2, y = 8
Output: true
Explanation: 23 is equal to 8Input: x = 10, y = 1
Output: true
Explanation: 100 is equal to 1Input: x = 10, y = 1001
Output: false
Table of Content
[Naive Approach] Repeated Multiplication Method
The idea is to start from 1 and repeatedly multiplying it by x. If the value becomes equal to y, then y is a power of x and if it exceeds y without matching, then it is not. This method relies on simple multiplication and avoids using logarithms or recursion.
Dry run for x = 2 and y = 8:
- Start with pow = 1.
- Multiply by 2, now pow = 2, which is still less than 8, so continue.
- Multiply again, now pow = 4, still less than 8, so continue.
- Multiply again, now pow = 8, which becomes equal to y.
- Since pow equals y, the function returns true.
Final conclusion: 8 is a power of 2.
#include <iostream>
using namespace std;
bool isPower(int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
int main()
{
cout << boolalpha;
cout << isPower(10, 1) << endl;
cout << isPower(1, 20) << endl;
cout << isPower(2, 128) << endl;
cout << isPower(2, 30) << endl;
return 0;
}
#include <stdio.h>
#include <stdbool.h>
bool isPower(int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
int main()
{
printf("%s\n", isPower(10, 1) ? "true" : "false");
printf("%s\n", isPower(1, 20) ? "true" : "false");
printf("%s\n", isPower(2, 128) ? "true" : "false");
printf("%s\n", isPower(2, 30) ? "true" : "false");
return 0;
}
class GfG {
public static boolean isPower(int x, int y) {
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
long pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
public static void main(String[] args) {
System.out.println(isPower(10, 1));
System.out.println(isPower(1, 20));
System.out.println(isPower(2, 128));
System.out.println(isPower(2, 30));
}
}
def isPower(x, y):
# The only power of 1 is 1 itself
if x == 1:
return y == 1
# Repeatedly compute power of x
pow = 1
while pow < y:
pow *= x
# Check if power of x becomes y
return pow == y
if __name__ == '__main__':
print("true" if isPower(10, 1) else "false")
print("true" if isPower(1, 20) else "false")
print("true" if isPower(2, 128) else "false")
print("true" if isPower(2, 30) else "false")
using System;
class GfG {
static bool isPower(int x, long y) {
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
long pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
static void Main() {
Console.WriteLine(isPower(10, 1) ? "true" : "false");
Console.WriteLine(isPower(1, 20) ? "true" : "false");
Console.WriteLine(isPower(2, 128) ? "true" : "false");
Console.WriteLine(isPower(2, 30) ? "true" : "false");
}
}
function isPower(x, y) {
// The only power of 1 is 1 itself
if (x === 1)
return y === 1;
// Repeatedly compute power of x
let pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return pow === y;
}
// Driver code
console.log(isPower(10, 1));
console.log(isPower(1, 20));
console.log(isPower(2, 128));
console.log(isPower(2, 30));
Output
true false true false
Time complexity: O(Logxy), The loop multiplies by x each time, reaching y in logarithmic steps.
Auxiliary space: O(1)
[Better Approach] Exponentiation and Binary Search Method
The idea is to increase the value very quickly using repeated squaring instead of normal multiplication, allowing it to reach close to y in fewer steps. If the value becomes equal to y, it returns true. If it goes beyond y, a binary search is used to check different possible powers of x and find if any of them exactly equals y.
Dry run for x = 2 and y = 8:
- Start with pow = 2 and i = 1.
- Square pow, now pow = 4 and i = 2, still less than 8 so continue.
- Square again, now pow = 16 and i = 4, this exceeds 8 so stop.
- Since pow is not equal to 8, perform binary search.
- Set range from low = 1 to high = 4 (possible powers).
- Find mid = 2, compute 22 = 4, which is less than 8, so move right.
- Find mid = 3, compute 23 = 8, which matches y.
- Since a matching power is found, the function returns true.
Final conclusion: 8 is a power of 2.
#include <iostream>
#include <cmath>
using namespace std;
bool isPower(int x, int y) {
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// If y is 1, then x^0 = 1
if (y == 1)
return true;
// Repeatedly compute power of x using squaring
int pow = x, i = 1;
while (pow < y) {
pow *= pow;
// track exponent growth
i *= 2;
}
if (pow == y)
return true;
// Apply binary search to find correct power
int low = x, high = pow;
while (low <= high) {
int mid = low + (high - low) / 2;
// Estimate exponent using logarithm
int exponent = (int)(log(mid) / log(x));
int result = (int)powl(x, exponent);
if (result == y)
return true;
// Move search space
if (result < y)
low = mid + 1;
else
high = mid - 1;
}
// If no power matches
return false;
}
int main() {
cout << boolalpha;
cout << isPower(10, 1) << endl;
cout << isPower(1, 20) << endl;
cout << isPower(2, 128) << endl;
cout << isPower(2, 30) << endl;
return 0;
}
class GfG {
public static boolean isPower(int x, int y) {
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// If y is 1, then x^0 = 1
if (y == 1)
return true;
// Repeatedly compute power of x using squaring
int pow = x, i = 1;
while (pow < y) {
pow *= pow;
// track exponent growth
i *= 2;
}
if (pow == y)
return true;
// Apply binary search to find correct power
int low = x, high = pow;
while (low <= high) {
int mid = low + (high - low) / 2;
// Estimate exponent using logarithm
int exponent = (int)(Math.log(mid) / Math.log(x));
int result = (int)Math.pow(x, exponent);
if (result == y)
return true;
// Move search space
if (result < y)
low = mid + 1;
else
high = mid - 1;
}
// If no power matches
return false;
}
public static void main(String[] args) {
System.out.println(isPower(10, 1) ? "true" : "false");
System.out.println(isPower(1, 20) ? "true" : "false");
System.out.println(isPower(2, 128) ? "true" : "false");
System.out.println(isPower(2, 30) ? "true" : "false");
}
}
import math
def isPower(x, y):
# The only power of 1 is 1 itself
if x == 1:
return y == 1
# If y is 1, then x^0 = 1
if y == 1:
return True
# Repeatedly compute power of x using squaring
pow = x
i = 1
while pow < y:
pow *= pow
# track exponent growth
i *= 2
if pow == y:
return True
# Apply binary search to find correct power
low, high = x, pow
while low <= high:
mid = low + (high - low) // 2
# Estimate exponent using logarithm
exponent = int(math.log(mid) / math.log(x))
result = int(x ** exponent)
if result == y:
return True
# Move search space
if result < y:
low = mid + 1
else:
high = mid - 1
# If no power matches
return False
if __name__ == "__main__":
print("true" if isPower(10, 1) else "false")
print("true" if isPower(1, 20) else "false")
print("true" if isPower(2, 128) else "false")
print("true" if isPower(2, 30) else "false")
using System;
class GfG {
public static bool isPower(int x, int y) {
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// If y is 1, then x^0 = 1
if (y == 1)
return true;
// Repeatedly compute power of x using squaring
int pow = x, i = 1;
while (pow < y) {
pow *= pow;
// track exponent growth
i *= 2;
}
if (pow == y)
return true;
// Apply binary search to find correct power
int low = x, high = pow;
while (low <= high) {
int mid = low + (high - low) / 2;
// Estimate exponent using logarithm
int exponent = (int)(Math.Log(mid) / Math.Log(x));
int result = (int)Math.Pow(x, exponent);
if (result == y)
return true;
// Move search space
if (result < y)
low = mid + 1;
else
high = mid - 1;
}
// If no power matches
return false;
}
static void Main() {
Console.WriteLine(isPower(10, 1) ? "true" : "false");
Console.WriteLine(isPower(1, 20) ? "true" : "false");
Console.WriteLine(isPower(2, 128) ? "true" : "false");
Console.WriteLine(isPower(2, 30) ? "true" : "false");
}
}
function isPower(x, y) {
// The only power of 1 is 1 itself
if (x === 1)
return (y === 1);
// If y is 1, then x^0 = 1
if (y === 1)
return true;
// Repeatedly compute power of x using squaring
let pow = x, i = 1;
while (pow < y) {
pow *= pow;
// track exponent growth
i *= 2;
}
if (pow === y)
return true;
// Apply binary search to find correct power
let low = x, high = pow;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
// Estimate exponent using logarithm
let exponent = Math.floor(Math.log(mid) / Math.log(x));
let result = Math.pow(x, exponent);
if (result === y)
return true;
// Move search space
if (result < y)
low = mid + 1;
else
high = mid - 1;
}
// If no power matches
return false;
}
// Driver code
console.log(isPower(10, 1) ? "true" : "false");
console.log(isPower(1, 20) ? "true" : "false");
console.log(isPower(2, 128) ? "true" : "false");
console.log(isPower(2, 30) ? "true" : "false");
Output
true false true false
Time Complexity: O(log y), First loop grows very fast (squaring) which takes log log y steps, then binary search takes log y, so overall ≈ log y.
Auxiliary Space: O(1)
[Expected Approach] Logarithmic Method - O(1) Time and O(1) Space
By applying the logarithm change of base formula, it computes logx(y)=log(y)/log(x). If the result is an integer, it means that y is an exact power of x, and the function returns true. Otherwise, it returns false.
Dry run for x = 2 and y = 8:
- Compute log(8) / log(2) using the logarithmic formula.
- The result comes out to be 3.
- This value represents the power such that 23 = 8.
- Now check whether this result is an integer.
- Since 3 is a whole number, it confirms that 8 is an exact power of 2.
- Therefore, the function returns true.
Final conclusion: 8 is a power of 2.
#include <iostream>
#include <cmath>
using namespace std;
bool isPower(int x, int y)
{
// Edge case: 1^k = 1 only
if (x == 1)
return y == 1;
// Edge case: x^0 = 1
if (y == 1)
return true;
// Compute logarithm
double res = log(y) / log(x);
// Compare with rounded value using a small
// tolerance to avoid floating point errors
return fabs(res - round(res)) < 1e-10;
}
int main()
{
cout << boolalpha;
cout << isPower(10, 1) << endl;
cout << isPower(1, 20) << endl;
cout << isPower(2, 128) << endl;
cout << isPower(2, 30) << endl;
return 0;
}
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
bool isPower(int x, int y)
{
// Edge case: 1^k = 1 only
if (x == 1)
return y == 1;
// Edge case: x^0 = 1
if (y == 1)
return true;
// Compute logarithm
double res = log(y) / log(x);
// Compare with rounded value using a small
// tolerance to avoid floating point errors
return fabs(res - round(res)) < 1e-10;
}
int main()
{
printf("%s\n", isPower(10, 1) ? "true" : "false");
printf("%s\n", isPower(1, 20) ? "true" : "false");
printf("%s\n", isPower(2, 128) ? "true" : "false");
printf("%s\n", isPower(2, 30) ? "true" : "false");
return 0;
}
import java.lang.Math;
class GfG {
static boolean isPower(int x, int y)
{
// Edge case: 1^k = 1 only
if (x == 1)
return y == 1;
// Edge case: x^0 = 1
if (y == 1)
return true;
// Compute logarithm
double res = Math.log(y) / Math.log(x);
// Compare with rounded value using a small
// tolerance to avoid floating point errors
return Math.abs(res - Math.round(res)) < 1e-10;
}
public static void main(String[] args)
{
System.out.println(isPower(10, 1) ? "true" : "false");
System.out.println(isPower(1, 20) ? "true" : "false");
System.out.println(isPower(2, 128) ? "true" : "false");
System.out.println(isPower(2, 30) ? "true" : "false");
}
}
import math
def isPower(x, y):
# Edge case: 1^k = 1 only
if x == 1:
return y == 1
# Edge case: x^0 = 1
if y == 1:
return True
# Compute logarithm
res = math.log(y) / math.log(x)
# Compare with rounded value using a small
# tolerance to avoid floating point errors
return abs(res - round(res)) < 1e-10
if __name__ == "__main__":
print("true" if isPower(10, 1) else "false")
print("true" if isPower(1, 20) else "false")
print("true" if isPower(2, 128) else "false")
print("true" if isPower(2, 30) else "false")
using System;
class GfG {
static bool isPower(int x, int y)
{
// Edge case: 1^k = 1 only
if (x == 1)
return y == 1;
// Edge case: x^0 = 1
if (y == 1)
return true;
// Compute logarithm
double res = Math.Log(y) / Math.Log(x);
// Compare with rounded value using a small
// tolerance to avoid floating point errors
return Math.Abs(res - Math.Round(res)) < 1e-10;
}
static void Main()
{
Console.WriteLine(isPower(10, 1) ? "true" : "false");
Console.WriteLine(isPower(1, 20) ? "true" : "false");
Console.WriteLine(isPower(2, 128) ? "true" : "false");
Console.WriteLine(isPower(2, 30) ? "true" : "false");
}
}
function isPower(x, y)
{
// Edge case: 1^k = 1 only
if (x === 1)
return y === 1;
// Edge case: x^0 = 1
if (y === 1)
return true;
// Compute logarithm
let res = Math.log(y) / Math.log(x);
// Compare with rounded value using a small tolerance to
// avoid floating point errors
return Math.abs(res - Math.round(res)) < 1e-10;
}
// Driver code
console.log(isPower(10, 1) ? "true" : "false");
console.log(isPower(1, 20) ? "true" : "false");
console.log(isPower(2, 128) ? "true" : "false");
console.log(isPower(2, 30) ? "true" : "false");
Output
true false true false