How do you check if a no is power of 2 in python?
Bit ManipulationsOne approach would be to use bit manipulations: Show
Explanation: every power of 2 has exactly 1 bit set to 1 (the bit in that number's log base-2 index). So when subtracting 1 from it, that bit flips to 0 and all preceding bits flip to 1. That makes these 2 numbers the inverse of each other so when AND-ing them, we will get 0 as the result. For example:
So, in conclusion, whenever we subtract one from a number, AND the result with the number itself, and that becomes 0 - that number is a power of 2! Of course, AND-ing anything with math functionsYou could always use math functions, but notice that using them without care could cause incorrect results:
Worth noting that for any
As noted above, for some numbers these functions are not accurate and actually give FALSE RESULTS:
This is because (Expanded) TimingSome time has passed since this question was asked and some new answers came up with the years. I decided to expand the timing to include all of them. According to the math docs, the So the results are:
The code I used for these measures can be recreated in this REPL (forked from this one). Given a positive integer n, write a function to find if it is a power of 2 or not Examples:
Find whether a given number is a power of 2 using logarithm:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
C
Java
Python3
C#
PHP
Javascript
Time Complexity: O(1) Find whether a given number is a power of 2 using the division operator:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
C
Java
Python3
C#
PHP
Javascript
Time Complexity: O(log N) Below is the recursive implementation of the above approach:C++
C
Java
Python3
C#
Javascript
Time Complexity: O(log N) Find whether a given number is a power of 2 by checking the count of set bits:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
Java
C#
Python3
Javascript
Time complexity: O(N) Find whether a given number is a power of 2 using the AND(&) operator:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
C
Java
Python3
C#
PHP
Javascript
Time Complexity: O(1) Find whether a given number is a power of 2 using the AND(&) and NOT(~) operator:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Time complexity: O(1) Find whether a given number is a power of 2 using Brian Kernighan’s algorithm:To solve the problem follow the below idea:
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Time Complexity: O(1) Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. How do you check if a number is a power of 2?To check if a given number is a power of 2, we can continuously divide the number by 2, on the condition that the given number is even. After the last possible division, if the value of the number is equal to 1, it is a power of 2.
How do you write to the power of 2 in Python?The power operator ( ** ) raises the left value to the power of the second value. For example: 2 ** 3 . The built-in pow() function does the same thing: it raises its first argument to the power of its second argument. Like this: pow(2, 3) .
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.
How do you check if a number is a power of 4 in Python?public static void main(String[] args). { int n = 256;. if (checkPowerOf4(n)) { System. out. println(n + " is a power of 4");. } else {. System. out. println(n + " is not a power of 4"); }. |