Python Challenges - 1: Exercise-5 with Solution
Write a Python program to check if an integer is the power of another integer.
Explanation:
Sample Solution:-
Python Code:
def is_Power[x, y]:
while [x%y == 0]:
x = x / y
return x == 1
print[is_Power[16, 2]]
print[is_Power[12, 2]]
print[is_Power[81, 3]]
Sample Output:
True False True
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to check if a number is a perfect square.
Next: Write a Python program to check if a number is a power of a given
base.
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 powers of x. If a power becomes equal to y, then y is a power, else not.
C++
#include
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
Javascript
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] +
"
"];
document.write[[isPower[1, 20] ? 1 : 0] +
"
"];
document.write[[isPower[2, 128] ? 1 : 0] +
"
"];
document.write[[isPower[2, 30] ? 1 : 0] +
"
"];
Output:
1 0 1 0
Time complexity of above solution is O[Logxy]
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
#include
using
namespace
std;
bool
isPower[
int
x,
int
y]
{
int
res1 =
log
[y] /
log
[x];
double
res2 =
log
[y] /
log
[x];
return
[res1 == res2];
}
int
main[]
{
cout
Javascript
function
isPower[x, y]
{
var
res1 = parseInt[Math.log[y]] /
parseInt[Math.log[x]];
var
res2 = Math.log[y] /
Math.log[x];
return
[res1 == res2];
}
if
[isPower[27, 729]]
document.write[
"1"
];
else
document.write[
"0"
];
Output :
1
Thanks to Gyayak Jain for suggesting this solution.
This article is
contributed by Manish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above