程序查找给定数字是否是 2 的幂
给定一个正整数,编写一个函数来判断它是否是 2 的幂。
例子 :
Input : n = 4
Output : Yes
22 = 4
Input : n = 7
Output : No
Input : n = 32
Output : Yes
25 = 32
1.一个简单的方法是简单地取以 2 为底的数字的对数,如果你得到一个整数,那么这个数字就是 2 的幂
C++
// C++ Program to find whether a
// no is power of two
#include
using namespace std;
// Function to check if x is power of 2
bool isPowerOfTwo(int n)
{
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
// Driver program
int main()
{
isPowerOfTwo(31)? cout<<"Yes"<
C
// C Program to find whether a
// no is power of two
#include
#include
#include
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
// Driver program
int main()
{
isPowerOfTwo(31)? printf("Yes\n"): printf("No\n");
isPowerOfTwo(64)? printf("Yes\n"): printf("No\n");
return 0;
}
// This code is contributed by bibhudhendra
Java
// Java Program to find whether a
// no is power of two
class GFG
{
/* Function to check if x is power of 2*/
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) ==
(int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
// Driver Code
public static void main(String[] args)
{
if(isPowerOfTwo(31))
System.out.println("Yes");
else
System.out.println("No");
if(isPowerOfTwo(64))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by mits
Python3
# Python3 Program to find
# whether a no is
# power of two
import math
# Function to check
# Log base 2
def Log2(x):
if x == 0:
return false;
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
C#
// C# Program to find whether
// a no is power of two
using System;
class GFG
{
/* Function to check if
x is power of 2*/
static bool isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.Ceiling((Math.Log(n) /
Math.Log(2)))) ==
(int)(Math.Floor(((Math.Log(n) /
Math.Log(2)))));
}
// Driver Code
public static void Main()
{
if(isPowerOfTwo(31))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
if(isPowerOfTwo(64))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
Javascript
C++
#include
using namespace std;
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
/*Driver code*/
int main()
{
isPowerOfTwo(31)? cout<<"Yes\n": cout<<"No\n";
isPowerOfTwo(64)? cout<<"Yes\n": cout<<"No\n";
return 0;
}
// This code is contributed by rathbhupendra
C
#include
#include
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
/*Driver program to test above function*/
int main()
{
isPowerOfTwo(31)? printf("Yes\n"): printf("No\n");
isPowerOfTwo(64)? printf("Yes\n"): printf("No\n");
return 0;
}
Java
// Java program to find whether
// a no is power of two
import java.io.*;
class GFG {
// Function to check if
// x is power of 2
static boolean isPowerOfTwo(int n)
{
if (n == 0)
return false;
while (n != 1)
{
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
// Driver program
public static void main(String args[])
{
if (isPowerOfTwo(31))
System.out.println("Yes");
else
System.out.println("No");
if (isPowerOfTwo(64))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Nikita tiwari.
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
C#
// C# program to find whether
// a no is power of two
using System;
class GFG
{
// Function to check if
// x is power of 2
static bool isPowerOfTwo(int n)
{
if (n == 0)
return false;
while (n != 1) {
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
// Driver program
public static void Main()
{
Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");
Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This code is contributed by Sam007
PHP
Javascript
C++
// C++ program for above approach
#include
using namespace std;
// Function which checks whether a
// number is a power of 2
bool powerOf2(int n)
{
// base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// all other odd numbers are not powers of 2
else if (n % 2 != 0 || n ==0)
return false;
// recursive function call
return powerOf2(n / 2);
}
// Driver Code
int main()
{
int n = 64;//True
int m = 12;//False
if (powerOf2(n) == 1)
cout << "True" << endl;
else cout << "False" << endl;
if (powerOf2(m) == 1)
cout << "True" << endl;
else
cout << "False" << endl;
}
//code contributed by Moukthik a.k.a rowdyninja
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function which checks
// whether a number is a
// power of 2
static boolean powerOf2(int n)
{
// base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// all other odd numbers are
// not powers of 2
else if (n % 2 != 0 ||
n ==0)
return false;
// recursive function call
return powerOf2(n / 2);
}
// Driver Code
public static void main(String[] args)
{
//True
int n = 64;
//False
int m = 12;
if (powerOf2(n) == true)
System.out.print("True" + "\n");
else System.out.print("False" + "\n");
if (powerOf2(m) == true)
System.out.print("True" + "\n");
else
System.out.print("False" + "\n");
}
}
// This code is contributed by Princi Singh
Python3
# Python program for above approach
# function which checks whether a
# number is a power of 2
def powerof2(n):
# base cases
# '1' is the only odd number
# which is a power of 2(2^0)
if n == 1:
return True
# all other odd numbers are not powers of 2
elif n%2 != 0 or n == 0:
return False
#recursive function call
return powerof2(n/2)
# Driver Code
if __name__ == "__main__":
print(powerof2(64)) #True
print(powerof2(12)) #False
#code contributed by Moukthik a.k.a rowdyninja
C#
// C# program for above approach
using System;
class GFG{
// Function which checks whether a
// number is a power of 2
static bool powerOf2(int n)
{
// Base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// All other odd numbers
// are not powers of 2
else if (n % 2 != 0 || n == 0)
return false;
// Recursive function call
return powerOf2(n / 2);
}
// Driver code
static void Main()
{
int n = 64;//True
int m = 12;//False
if (powerOf2(n))
{
Console.Write("True" + "\n");
}
else
{
Console.Write("False" + "\n");
}
if (powerOf2(m))
{
Console.Write("True");
}
else
{
Console.Write("False");
}
}
}
// This code is contributed by rutvik_56
Javascript
C++
#include
using namespace std;
#define bool int
/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
/*Driver code*/
int main()
{
isPowerOfTwo(31)? cout<<"Yes\n": cout<<"No\n";
isPowerOfTwo(64)? cout<<"Yes\n": cout<<"No\n";
return 0;
}
// This code is contributed by rathbhupendra
C
#include
#define bool int
/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
/*Driver program to test above function*/
int main()
{
isPowerOfTwo(31)? printf("Yes\n"): printf("No\n");
isPowerOfTwo(64)? printf("Yes\n"): printf("No\n");
return 0;
}
Java
// Java program to efficiently
// check for power for 2
class Test
{
/* Method to check if x is power of 2*/
static boolean isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x!=0 && ((x&(x-1)) == 0);
}
// Driver method
public static void main(String[] args)
{
System.out.println(isPowerOfTwo(31) ? "Yes" : "No");
System.out.println(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This program is contributed by Gaurav Miglani
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
C#
// C# program to efficiently
// check for power for 2
using System;
class GFG
{
// Method to check if x is power of 2
static bool isPowerOfTwo (int x)
{
// First x in the below expression
// is for the case when x is 0
return x != 0 && ((x & (x - 1)) == 0);
}
// Driver method
public static void Main()
{
Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");
Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This code is contributed by Sam007
PHP
Javascript
C++
#include
using namespace std;
/* Function to check if x is power of 2*/
bool isPowerofTwo(long long n)
{
if (n == 0)
return 0;
if ((n & (~(n - 1))) == n)
return 1;
return 0;
}
/*Driver code*/
int main()
{
isPowerofTwo(30) ? cout << "Yes\n" : cout << "No\n";
isPowerofTwo(128) ? cout << "Yes\n" : cout << "No\n";
return 0;
}
// This code is contributed by Sachin
Java
// Java program of the above approach
import java.io.*;
class GFG {
// Function to check if x is power of 2
static boolean isPowerofTwo(int n)
{
if (n == 0)
return false;
if ((n & (~(n - 1))) == n)
return true;
return false;
}
public static void main(String[] args)
{
if (isPowerofTwo(30) == true)
System.out.println("Yes");
else
System.out.println("No");
if (isPowerofTwo(128) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by rajsanghavi9.
Python3
# Python program of the above approach
# Function to check if x is power of 2*/
def isPowerofTwo(n):
if (n == 0):
return 0
if ((n & (~(n - 1))) == n):
return 1
return 0
# Driver code
if(isPowerofTwo(30)):
print('Yes')
else:
print('No')
if(isPowerofTwo(128)):
print('Yes')
else:
print('No')
# This code is contributed by shivanisinghss2110
C#
// C# program of the above approach
using System;
public class GFG {
// Function to check if x is power of 2
static bool isPowerofTwo(int n)
{
if (n == 0)
return false;
if ((n & (~(n - 1))) == n)
return true;
return false;
}
public static void Main(String[] args)
{
if (isPowerofTwo(30) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
if (isPowerofTwo(128) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code contributed by gauravrajput1
Javascript
输出:
No
Yes
时间复杂度: O(1)
辅助空间: O(1)
2.另一种解决方案是不断将数字除以 2,即迭代地执行 n = n/2。在任何迭代中,如果 n%2 变为非零且 n 不是 1,则 n 不是 2 的幂。如果 n 变为 1,则它是 2 的幂。
C++
#include
using namespace std;
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
/*Driver code*/
int main()
{
isPowerOfTwo(31)? cout<<"Yes\n": cout<<"No\n";
isPowerOfTwo(64)? cout<<"Yes\n": cout<<"No\n";
return 0;
}
// This code is contributed by rathbhupendra
C
#include
#include
/* Function to check if x is power of 2*/
bool isPowerOfTwo(int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
/*Driver program to test above function*/
int main()
{
isPowerOfTwo(31)? printf("Yes\n"): printf("No\n");
isPowerOfTwo(64)? printf("Yes\n"): printf("No\n");
return 0;
}
Java
// Java program to find whether
// a no is power of two
import java.io.*;
class GFG {
// Function to check if
// x is power of 2
static boolean isPowerOfTwo(int n)
{
if (n == 0)
return false;
while (n != 1)
{
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
// Driver program
public static void main(String args[])
{
if (isPowerOfTwo(31))
System.out.println("Yes");
else
System.out.println("No");
if (isPowerOfTwo(64))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Nikita tiwari.
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
C#
// C# program to find whether
// a no is power of two
using System;
class GFG
{
// Function to check if
// x is power of 2
static bool isPowerOfTwo(int n)
{
if (n == 0)
return false;
while (n != 1) {
if (n % 2 != 0)
return false;
n = n / 2;
}
return true;
}
// Driver program
public static void Main()
{
Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");
Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This code is contributed by Sam007
PHP
Javascript
输出 :
No
Yes
时间复杂度: O(log 2 n)
辅助空间: O(1)
3.另一种方法是使用这种简单的递归解决方案。它使用与上述迭代解决方案相同的逻辑,但使用递归而不是迭代。
C++
// C++ program for above approach
#include
using namespace std;
// Function which checks whether a
// number is a power of 2
bool powerOf2(int n)
{
// base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// all other odd numbers are not powers of 2
else if (n % 2 != 0 || n ==0)
return false;
// recursive function call
return powerOf2(n / 2);
}
// Driver Code
int main()
{
int n = 64;//True
int m = 12;//False
if (powerOf2(n) == 1)
cout << "True" << endl;
else cout << "False" << endl;
if (powerOf2(m) == 1)
cout << "True" << endl;
else
cout << "False" << endl;
}
//code contributed by Moukthik a.k.a rowdyninja
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function which checks
// whether a number is a
// power of 2
static boolean powerOf2(int n)
{
// base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// all other odd numbers are
// not powers of 2
else if (n % 2 != 0 ||
n ==0)
return false;
// recursive function call
return powerOf2(n / 2);
}
// Driver Code
public static void main(String[] args)
{
//True
int n = 64;
//False
int m = 12;
if (powerOf2(n) == true)
System.out.print("True" + "\n");
else System.out.print("False" + "\n");
if (powerOf2(m) == true)
System.out.print("True" + "\n");
else
System.out.print("False" + "\n");
}
}
// This code is contributed by Princi Singh
Python3
# Python program for above approach
# function which checks whether a
# number is a power of 2
def powerof2(n):
# base cases
# '1' is the only odd number
# which is a power of 2(2^0)
if n == 1:
return True
# all other odd numbers are not powers of 2
elif n%2 != 0 or n == 0:
return False
#recursive function call
return powerof2(n/2)
# Driver Code
if __name__ == "__main__":
print(powerof2(64)) #True
print(powerof2(12)) #False
#code contributed by Moukthik a.k.a rowdyninja
C#
// C# program for above approach
using System;
class GFG{
// Function which checks whether a
// number is a power of 2
static bool powerOf2(int n)
{
// Base cases
// '1' is the only odd number
// which is a power of 2(2^0)
if (n == 1)
return true;
// All other odd numbers
// are not powers of 2
else if (n % 2 != 0 || n == 0)
return false;
// Recursive function call
return powerOf2(n / 2);
}
// Driver code
static void Main()
{
int n = 64;//True
int m = 12;//False
if (powerOf2(n))
{
Console.Write("True" + "\n");
}
else
{
Console.Write("False" + "\n");
}
if (powerOf2(m))
{
Console.Write("True");
}
else
{
Console.Write("False");
}
}
}
// This code is contributed by rutvik_56
Javascript
True
False
时间复杂度: O(log 2 n)
辅助空间: O(log 2 n)
4.两个数的所有幂只有一位集合。所以数数。设置位的数量,如果您得到 1,则该数字是 2 的幂。请参阅以整数计数设置位以计算设置位。
5.如果我们将 2 的幂减去 1,则唯一设置位之后的所有未设置位都将被设置;设置位变为未设置。
例如对于 4 ( 100) 和 16(10000),我们在减去 1 后得到以下结果
3 –> 011
15 –> 01111
因此,如果数字 n 是 2 的幂,则 n 和 n-1 的按位 & 将为零。我们可以根据 n&(n-1) 的值说 n 是 2 的幂或不是 2 的幂。当 n 为 0 时,表达式 n&(n-1) 将不起作用。为了处理这种情况,我们的表达式将变为 n& (!n&(n-1)) (感谢 https://www.geeksforgeeks.org/program - 查找是否不属于二的幂/穆罕默德添加这个案例)。
下面是这个方法的实现。
时间复杂度:O(1)
空间复杂度:O(1)
C++
#include
using namespace std;
#define bool int
/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
/*Driver code*/
int main()
{
isPowerOfTwo(31)? cout<<"Yes\n": cout<<"No\n";
isPowerOfTwo(64)? cout<<"Yes\n": cout<<"No\n";
return 0;
}
// This code is contributed by rathbhupendra
C
#include
#define bool int
/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
/*Driver program to test above function*/
int main()
{
isPowerOfTwo(31)? printf("Yes\n"): printf("No\n");
isPowerOfTwo(64)? printf("Yes\n"): printf("No\n");
return 0;
}
Java
// Java program to efficiently
// check for power for 2
class Test
{
/* Method to check if x is power of 2*/
static boolean isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x!=0 && ((x&(x-1)) == 0);
}
// Driver method
public static void main(String[] args)
{
System.out.println(isPowerOfTwo(31) ? "Yes" : "No");
System.out.println(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This program is contributed by Gaurav Miglani
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
C#
// C# program to efficiently
// check for power for 2
using System;
class GFG
{
// Method to check if x is power of 2
static bool isPowerOfTwo (int x)
{
// First x in the below expression
// is for the case when x is 0
return x != 0 && ((x & (x - 1)) == 0);
}
// Driver method
public static void Main()
{
Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");
Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");
}
}
// This code is contributed by Sam007
PHP
Javascript
输出 :
No
Yes
时间复杂度: O(1)
辅助空间: O(1)
6 .另一种方法是使用逻辑找到给定数字的最右边的位集。
C++
#include
using namespace std;
/* Function to check if x is power of 2*/
bool isPowerofTwo(long long n)
{
if (n == 0)
return 0;
if ((n & (~(n - 1))) == n)
return 1;
return 0;
}
/*Driver code*/
int main()
{
isPowerofTwo(30) ? cout << "Yes\n" : cout << "No\n";
isPowerofTwo(128) ? cout << "Yes\n" : cout << "No\n";
return 0;
}
// This code is contributed by Sachin
Java
// Java program of the above approach
import java.io.*;
class GFG {
// Function to check if x is power of 2
static boolean isPowerofTwo(int n)
{
if (n == 0)
return false;
if ((n & (~(n - 1))) == n)
return true;
return false;
}
public static void main(String[] args)
{
if (isPowerofTwo(30) == true)
System.out.println("Yes");
else
System.out.println("No");
if (isPowerofTwo(128) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by rajsanghavi9.
Python3
# Python program of the above approach
# Function to check if x is power of 2*/
def isPowerofTwo(n):
if (n == 0):
return 0
if ((n & (~(n - 1))) == n):
return 1
return 0
# Driver code
if(isPowerofTwo(30)):
print('Yes')
else:
print('No')
if(isPowerofTwo(128)):
print('Yes')
else:
print('No')
# This code is contributed by shivanisinghss2110
C#
// C# program of the above approach
using System;
public class GFG {
// Function to check if x is power of 2
static bool isPowerofTwo(int n)
{
if (n == 0)
return false;
if ((n & (~(n - 1))) == n)
return true;
return false;
}
public static void Main(String[] args)
{
if (isPowerofTwo(30) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
if (isPowerofTwo(128) == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code contributed by gauravrajput1
Javascript
No
Yes
时间复杂度: O(1)
空间复杂度: O(1)