How do you check if an integer is a power to another integer in python?

Last update on August 19 2022 21:50:48 [UTC/GMT +8 hours]

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
 


How do you check if a number is a power of another Python?

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.

How do you check if an integer is a power of 2 in Python?

A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2..
Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. ... .
All power of two numbers have only one bit set..

How do you evaluate a power in Python?

Python pow[] Function The pow[] function returns the value of x to the power of y [xy]. If a third parameter is present, it returns x to the power of y, modulus z.

Chủ Đề