Given two positive numbers x and y, check if y is a power of x or not.
Examples :
Input: x = 10, y = 1
Output: True
x^0 = 1
Input: x = 10, y = 1000
Output: True
x^3 = 1
Input: x = 10, y = 1001
Output: False
A simple solution is to repeatedly compute the powers of x. If a power becomes equal to y, then y is a power, else not.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower(int x, long int y)
{
if (x == 1)
return (y == 1);
long int pow = 1;
while (pow < y)
pow *= x;
return (pow == y);
}
int main()
{
cout << isPower(10, 1) << endl;
cout << isPower(1, 20) << endl;
cout << isPower(2, 128) << endl;
cout << isPower(2, 30) << endl;
return 0;
}
|
Java
public class Test {
public static void main(String[] args)
{
System.out.println(isPower(10, 1) ? 1 : 0);
System.out.println(isPower(1, 20) ? 1 : 0);
System.out.println(isPower(2, 128) ? 1 : 0);
System.out.println(isPower(2, 30) ? 1 : 0);
}
public static boolean isPower(int x, int y)
{
if (x == 1)
return (y == 1);
int pow = 1;
while (pow < y)
pow = pow * x;
return (pow == y);
}
}
|
Python3
def isPower (x, y):
if (x == 1):
return (y == 1)
pow = 1
while (pow < y):
pow = pow * x
return (pow == y)
if(isPower(10, 1)):
print(1)
else:
print(0)
if(isPower(1, 20)):
print(1)
else:
print(0)
if(isPower(2, 128)):
print(1)
else:
print(0)
if(isPower(2, 30)):
print(1)
else:
print(0)
|
C#
using System;
class GFG
{
public static bool isPower (int x, int y)
{
if (x == 1)
return (y == 1);
int pow = 1;
while (pow < y)
pow = pow * x;
return (pow == y);
}
public static void Main ()
{
Console.WriteLine(isPower(10, 1) ? 1 : 0);
Console.WriteLine(isPower(1, 20) ? 1 : 0);
Console.WriteLine(isPower(2, 128) ? 1 : 0);
Console.WriteLine(isPower(2, 30) ? 1 : 0);
}
}
|
PHP
<?php
function isPower($x, $y)
{
if ($x == 1)
return ($y == 1 ? 1 : 0);
$pow = 1;
while ($pow < $y)
$pow *= $x;
return ($pow == $y ? 1 : 0);
}
echo isPower(10, 1) . "\n";
echo isPower(1, 20) . "\n";
echo isPower(2, 128) . "\n";
echo isPower(2, 30) . "\n";
?>
|
Javascript
<script>
function isPower(x, y)
{
if (x == 1)
return (y == 1);
let pow = 1;
while (pow < y)
pow = pow * x;
return (pow == y);
}
document.write((isPower(10, 1) ? 1 : 0) + "<br/>");
document.write((isPower(1, 20) ? 1 : 0) + "<br/>");
document.write((isPower(2, 128) ? 1 : 0) + "<br/>");
document.write((isPower(2, 30) ? 1 : 0) + "<br/>");
</script>
|
Time complexity: O(Logxy)
Auxiliary space: O(1)
Optimization:
We can optimize above solution to work in O(Log Log y). The idea is to do squaring of power instead of multiplying it with x, i.e., compare y with x^2, x^4, x^8, …etc. If x becomes equal to y, return true. If x becomes more than y, then we do binary search for power of x between previous power and current power, i.e., between x^i and x^(i/2).
Following are detailed step.
1) Initialize pow = x, i = 1
2) while (pow < y)
{
pow = pow*pow
i *= 2
}
3) If pow == y
return true;
4) Else construct an array of powers
from x^i to x^(i/2)
5) Binary Search for y in array constructed
in step 4. If not found, return false.
Else return true.
Alternate Solution :
The idea is to take log of y in base x. If it turns out to be an integer, we return true. Else false.
C++
#include <iostream>
#include <math.h>
using namespace std;
bool isPower(int x, int y)
{
float res1 = log(y) / log(x);
return res1==floor(res1);
}
int main()
{
cout << isPower(2, 128) << endl;
return 0;
}
|
Java
class GFG
{
static boolean isPower(int x,
int y)
{
float res1 = (float)(Math.log(y) /
Math.log(x));
return (res1%1==0);
}
public static void main(String args[])
{
if(isPower(2, 128))
System.out.println("1");
else
System.out.println("0");
}
}
|
Python3
import math
def is_power(x, y):
res1 = math.log(y) / math.log(x)
res2 = math.log(y) / math.log(x)
return res1 == res2
if __name__ == "__main__":
print(is_power(2, 128))
|
C#
using System;
namespace ConsoleApp1
{
class Program
{
static bool IsPower(int x, int y)
{
double res1 = Math.Log(y) / Math.Log(x);
double res2 = Math.Log(y) / Math.Log(x);
return (res1 == res2);
}
static void Main(string[] args)
{
if (IsPower(2, 128))
Console.WriteLine("1");
else
Console.WriteLine("0");
}
}
}
|
PHP
<?php
function isPower($x, $y)
{
$res1 = log($y) / log($x);
$res2 = log($y) / log($x);
return ($res1 == $res2);
}
echo isPower(2, 128) ;
?>
|
Javascript
function isPower(x, y) {
const res1 = Math.log(y) / Math.log(x);
const res2 = Math.log(y) / Math.log(x);
return (res1 === res2);
}
console.log(isPower(2, 128));
|
Time complexity: O(log Y)
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
24 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...