📌  相关文章
📜  Python程序判断一个否是否是二的幂

📅  最后修改于: 2022-05-13 01:56:56.294000             🧑  作者: Mango

Python程序判断一个否是否是二的幂

给定一个正整数,编写一个函数来判断它是否是 2 的幂。
例子 :

Input : n = 4
Output : Yes
22 = 4

Input : n = 7
Output : No

Input : n = 32
Output : Yes
25 = 32

1.一个简单的方法是简单地取以 2 为底的数字的对数,如果你得到一个整数,那么数字就是 2 的幂。

Python3
# Python3 Program to find
# whether a no is
# power of two
import math
 
# Function to check
# Log base 2
def Log2(x):
    return (math.log10(x) /
            math.log10(2));
 
# Function to check
# if x is power of 2
def isPowerOfTwo(n):
    return (math.ceil(Log2(n)) == math.floor(Log2(n)));
 
# Driver Code
if(isPowerOfTwo(31)):
    print("Yes");
else:
    print("No");
 
if(isPowerOfTwo(64)):
    print("Yes");
else:
    print("No");
     
# This code is contributed
# by mits


Python3
# Python program to check if given
# number is power of 2 or not
 
# Function to check if x is power of 2
def isPowerOfTwo(n):
    if (n == 0):
        return False
    while (n != 1):
            if (n % 2 != 0):
                return False
            n = n // 2
             
    return True
 
# Driver code
if(isPowerOfTwo(31)):
    print('Yes')
else:
    print('No')
if(isPowerOfTwo(64)):
    print('Yes')
else:
    print('No')
 
# This code is contributed by Danish Raza


Python3
# Python program to check if given
# number is power of 2 or not
 
# Function to check if x is power of 2
def isPowerOfTwo (x):
 
    # First x in the below expression
    # is for the case when x is 0
    return (x and (not(x & (x - 1))) )
 
# Driver code
if(isPowerOfTwo(31)):
    print('Yes')
else:
    print('No')
     
if(isPowerOfTwo(64)):
    print('Yes')
else:
    print('No')
     
# This code is contributed by Danish Raza


输出:
No
Yes

时间复杂度: O(log 2 n)

辅助空间: O(1)

2.另一种解决方案是不断将数字除以 2,即迭代地执行 n = n/2。在任何迭代中,如果 n%2 变为非零且 n 不是 1,则 n 不是 2 的幂。如果 n 变为 1,则它是 2 的幂。

Python3

# Python program to check if given
# number is power of 2 or not
 
# Function to check if x is power of 2
def isPowerOfTwo(n):
    if (n == 0):
        return False
    while (n != 1):
            if (n % 2 != 0):
                return False
            n = n // 2
             
    return True
 
# Driver code
if(isPowerOfTwo(31)):
    print('Yes')
else:
    print('No')
if(isPowerOfTwo(64)):
    print('Yes')
else:
    print('No')
 
# This code is contributed by Danish Raza
输出:
No
Yes

3.两个数的所有幂都只有一个位集。所以数数。设置位的数量,如果您得到 1,则数字是 2 的幂。请参阅以整数计数设置位以计算设置位。
4.如果我们将 2 的幂减 1,则唯一设置位之后的所有未设置位都将被设置;设置位变为未设置。
例如对于 4 ( 100) 和 16(10000),我们在减去 1 后得到以下结果
3 –> 011
15 –> 01111
因此,如果数字 n 是 2 的幂,则 n 和 n-1 的按位 & 将为零。我们可以说 n 是 2 的幂或不是基于 n&(n-1) 的值。当 n 为 0 时,表达式 n&(n-1) 将不起作用。为了处理这种情况,我们的表达式将变为 n& (!n&(n-1)) (感谢 https://www.geeksforgeeks.org/program - 查找是否不属于二的幂/穆罕默德添加这个案例)。
下面是这个方法的实现。

Python3

# Python program to check if given
# number is power of 2 or not
 
# Function to check if x is power of 2
def isPowerOfTwo (x):
 
    # First x in the below expression
    # is for the case when x is 0
    return (x and (not(x & (x - 1))) )
 
# Driver code
if(isPowerOfTwo(31)):
    print('Yes')
else:
    print('No')
     
if(isPowerOfTwo(64)):
    print('Yes')
else:
    print('No')
     
# This code is contributed by Danish Raza   
输出:
No
Yes

有关详细信息,请参阅有关程序的完整文章以查找否是否是二的幂!