📌  相关文章
📜  给定一个巨大的数字,检查它是否为2的幂。

📅  最后修改于: 2021-05-04 15:52:40             🧑  作者: Mango

查找给定数字num是否为2的幂。更具体地说,找到给定数是否可以表示为2 ^ k,其中k> =1。如果该数是2的幂,则返回1,否则返回0。
笔记 :

  • 给定数字的位数,即(num)可以大于100。
  • 非零数字之前没有前导零。例如0000128不在输入中。/li>

例子:

Input : 3 
Output : 0
2^0 = 1 where as k >= 1

Input : 128
Output : 1

方法1:使用字符串
想法是将大数(表示为字符串)重复除以2。要执行除法,我们从右到左遍历数字。如果最后一位数字本身不能被2整除,则返回0。否则,我们采用除法的学校方法。

C
/*  C program to find whether a number is power of
    2 or not */
#include 
#include 
 
// returns 1 when str is power of 2
// return 0 when str is not a power of 2
int isPowerOf2(char* str)
{
    int len_str = strlen(str);
 
    // sum stores the intermediate dividend while
    // dividing.
    int num = 0;
 
    // if the input is "1" then return 0
    // because 2^k = 1 where k >= 1 and here k = 0
    if (len_str == 1 && str[len_str - 1] == '1')
        return 0;
 
    // Divide the number until it gets reduced to 1
    // if we are successfully able to reduce the number
    // to 1 it means input string is power of two if in
    // between an odd number appears at the end it means
    // string is not divisible by two hence not a power
    // of 2.
    while (len_str != 1 || str[len_str - 1] != '1') {
 
        // if the last digit is odd then string is not
        // divisible by 2 hence not a power of two
        // return 0.
        if ((str[len_str - 1] - '0') % 2 == 1)
            return 0;
 
        // divide the whole string by 2. i is used to
        // track index in current number. j is used to
        // track index for next iteration.
        for (int i = 0, j = 0; i < len_str; i++) {
            num = num * 10 + str[i] - '0';
             
            // if num < 2 then we have to take another digit
            // to the right of A[i] to make it bigger than
            // A[i]. E. g. 214 / 2 --> 107
            if (num < 2) {
 
                // if it's not the first index. E.g 214
                // then we have to include 0.
                if (i != 0)
                    str[j++] = '0';               
 
                // for eg. "124" we will not write 064
                // so if it is the first index just ignore
                continue;
            }
 
            str[j++] = (int)(num / 2) + '0';
            num = (num) - (num / 2) * 2;
        }
 
        str[j] = '\0';
 
        // After every division by 2 the
        // length of string is changed.
        len_str = j;
    }
 
    // if the string reaches to 1 then the str is
    // a power of 2.
    return 1;
}
 
// Driver code.
int main()
{
    char str1[] = "12468462246684202468024"
                     "6842024662202000002";
    char str2[] = "1";
    char str3[] = "128";
 
    printf("%d\n%d\n%d", isPowerOf2(str1),
           isPowerOf2(str2), isPowerOf2(str3));
 
    return 0;
}


Java
/* Java program to find whether a number 
    is power of 2 or not */
import java.util.*;
 
class GFG
{
     
// returns 1 when str is power of 2
// return 0 when str is not a power of 2
static int isPowerOf2(String s)
{
    char []str = s.toCharArray();
    int len_str = s.length();
 
    // sum stores the intermediate dividend while
    // dividing.
    int num = 0;
 
    // if the input is "1" then return 0
    // because 2^k = 1 where k >= 1 and here k = 0
    if (len_str == 1 && str[len_str - 1] == '1')
        return 0;
 
    // Divide the number until it gets reduced to 1
    // if we are successfully able to reduce the number
    // to 1 it means input string is power of two if in
    // between an odd number appears at the end it means
    // string is not divisible by two hence not a power
    // of 2.
    while (len_str != 1 || str[len_str - 1] != '1')
    {
 
        // if the last digit is odd then string is not
        // divisible by 2 hence not a power of two
        // return 0.
        if ((str[len_str - 1] - '0') % 2 == 1)
            return 0;
 
        // divide the whole string by 2. i is used to
        // track index in current number. j is used to
        // track index for next iteration.
        int j = 0;
        for (int i = 0; i < len_str; i++)
        {
            num = num * 10 + (int)str[i] - (int)'0';
             
            // if num < 2 then we have to take another digit
            // to the right of A[i] to make it bigger than
            // A[i]. E. g. 214 / 2 --> 107
            if (num < 2)
            {
 
                // if it's not the first index. E.g 214
                // then we have to include 0.
                if (i != 0)
                    str[j++] = '0';    
 
                // for eg. "124" we will not write 064
                // so if it is the first index just ignore
                continue;
            }
 
            str[j++] = (char)((int)(num / 2) + (int)'0');
            num = (num) - (num / 2) * 2;
        }
 
        str[j] = '\0';
 
        // After every division by 2 the
        // length of string is changed.
        len_str = j;
    }
 
    // if the string reaches to 1 then the str is
    // a power of 2.
    return 1;
}
 
// Driver code.
public static void main (String[] args)
{
    String str1 = "124684622466842024680246842024662202000002";
    String str2 = "1";
    String str3 = "128";
 
    System.out.println(isPowerOf2(str1) +
                "\n"+isPowerOf2(str2) +
                "\n"+isPowerOf2(str3));
}
}
 
// This code is contributed by mits


Python3
# Python3 program to find whether a number
# is power of 2 or not
 
# returns 1 when str is power of 2
# return 0 when str is not a power of 2
def isPowerOf2(sttr):
 
    len_str = len(sttr);
    sttr=list(sttr);
 
    # sum stores the intermediate dividend
    # while dividing.
    num = 0;
 
    # if the input is "1" then return 0
    # because 2^k = 1 where k >= 1 and here k = 0
    if (len_str == 1 and sttr[len_str - 1] == '1'):
        return 0;
 
    # Divide the number until it gets reduced to 1
    # if we are successfully able to reduce the number
    # to 1 it means input string is power of two if in
    # between an odd number appears at the end it means
    # string is not divisible by two hence not a power
    # of 2.
    while (len_str != 1 or sttr[len_str - 1] != '1'):
 
        # if the last digit is odd then string is not
        # divisible by 2 hence not a power of two
        # return 0.
        if ((ord(sttr[len_str - 1]) - ord('0')) % 2 == 1):
            return 0;
 
        # divide the whole string by 2. i is used to
        # track index in current number. j is used to
        # track index for next iteration.
        j = 0;
        for i in range(len_str):
            num = num * 10 + (ord(sttr[i]) - ord('0'));
             
            # if num < 2 then we have to take another digit
            # to the right of A[i] to make it bigger than
            # A[i]. E. g. 214 / 2 --> 107
            if (num < 2):
 
                # if it's not the first index. E.g 214
                # then we have to include 0.
                if (i != 0):
                    sttr[j] = '0';
                    j += 1;
 
                # for eg. "124" we will not write 064
                # so if it is the first index just ignore
                continue;
 
            sttr[j] = chr((num // 2) + ord('0'));
            j += 1;
            num = (num) - (num // 2) * 2;
 
        # After every division by 2 the
        # length of string is changed.
        len_str = j;
 
    # if the string reaches to 1 then the
    # str is a power of 2.
    return 1;
 
# Driver code.
str1 = "124684622466842024680246842024662202000002";
str2 = "1";
str3 = "128";
 
print("", isPowerOf2(str1), "\n",
          isPowerOf2(str2), "\n",
          isPowerOf2(str3));
 
# This code is contributed by mits


C#
/* C# program to find whether a number is power of
    2 or not */
using System;
 
class GFG
{
     
// returns 1 when str is power of 2
// return 0 when str is not a power of 2
static int isPowerOf2(string s)
{
    char []str = s.ToCharArray();
    int len_str = str.Length;
 
    // sum stores the intermediate dividend while
    // dividing.
    int num = 0;
 
    // if the input is "1" then return 0
    // because 2^k = 1 where k >= 1 and here k = 0
    if (len_str == 1 && str[len_str - 1] == '1')
        return 0;
 
    // Divide the number until it gets reduced to 1
    // if we are successfully able to reduce the number
    // to 1 it means input string is power of two if in
    // between an odd number appears at the end it means
    // string is not divisible by two hence not a power
    // of 2.
    while (len_str != 1 || str[len_str - 1] != '1')
    {
 
        // if the last digit is odd then string is not
        // divisible by 2 hence not a power of two
        // return 0.
        if ((str[len_str - 1] - '0') % 2 == 1)
            return 0;
 
        // divide the whole string by 2. i is used to
        // track index in current number. j is used to
        // track index for next iteration.
        int j = 0;
        for (int i = 0; i < len_str; i++)
        {
            num = num * 10 + (int)str[i] - (int)'0';
             
            // if num < 2 then we have to take another digit
            // to the right of A[i] to make it bigger than
            // A[i]. E. g. 214 / 2 --> 107
            if (num < 2)
            {
 
                // if it's not the first index. E.g 214
                // then we have to include 0.
                if (i != 0)
                    str[j++] = '0';        
 
                // for eg. "124" we will not write 064
                // so if it is the first index just ignore
                continue;
            }
 
            str[j++] = (char)((int)(num / 2) + (int)'0');
            num = (num) - (num / 2) * 2;
        }
 
        str[j] = '\0';
 
        // After every division by 2 the
        // length of string is changed.
        len_str = j;
    }
 
    // if the string reaches to 1 then the str is
    // a power of 2.
    return 1;
}
 
// Driver code.
static void Main()
{
    string str1 = "124684622466842024680246842024662202000002";
    string str2 = "1";
    string str3 = "128";
 
    Console.Write(isPowerOf2(str1) +
                "\n"+isPowerOf2(str2) +
                "\n"+isPowerOf2(str3));
}
}
 
// This code is contributed by mits


PHP
= 1 and here k = 0
    if ($len_str == 1 && $str[$len_str - 1] == '1')
        return 0;
 
    // Divide the number until it gets reduced to 1
    // if we are successfully able to reduce the number
    // to 1 it means input string is power of two if in
    // between an odd number appears at the end it means
    // string is not divisible by two hence not a power
    // of 2.
    while ($len_str != 1 || $str[$len_str - 1] != '1')
    {
 
        // if the last digit is odd then string is not
        // divisible by 2 hence not a power of two
        // return 0.
        if (ord($str[$len_str - 1] - '0') % 2 == 1)
            return 0;
 
        // divide the whole string by 2. i is used to
        // track index in current number. j is used to
        // track index for next iteration.
        $j=0;
        for ($i = 0; $i < $len_str; $i++)
        {
            $num = $num * 10 + (ord($str[$i]) - ord('0'));
             
            // if num < 2 then we have to take another digit
            // to the right of A[i] to make it bigger than
            // A[i]. E. g. 214 / 2 --> 107
            if ($num < 2)
            {
 
                // if it's not the first index. E.g 214
                // then we have to include 0.
                if ($i != 0)
                    $str[$j++] = '0';        
 
                // for eg. "124" we will not write 064
                // so if it is the first index just ignore
                continue;
            }
 
            $str[$j++] = chr((int)($num / 2) + ord('0'));
            $num = ($num) - (int)($num / 2) * 2;
        }
 
 
        // After every division by 2 the
        // length of string is changed.
        $len_str = $j;
    }
 
    // if the string reaches to 1 then the str is
    // a power of 2.
    return 1;
}
 
    // Driver code.
    $str1 = "124684622466842024680246842024662202000002";
    $str2 = "1";
    $str3 = "128";
 
    print(isPowerOf2($str1)."\n".isPowerOf2($str2)."\n".isPowerOf2($str3));
 
// This code is contributed by mits
?>
 
?>


C++
// C++ program to find whether a number
// is power of 2 or not
#include 
#include 
 
using namespace std;
using namespace boost::multiprecision;
 
// Function to check whether a
// number is power of 2 or not
bool ispowerof2 ( cpp_int num )
{
    if ( ( num & ( num - 1 ) ) == 0 )
        return 1;
    return 0;   
}
 
// Driver function
int main()
{
    cpp_int num = 549755813888;
    cout << ispowerof2 ( num ) << endl;
    return 0;
} 
 
// This code is contributed by  Aditya Gupta 4


Java
// Java program to find
// whether a number
// is power of 2 or not
class GfG
{
 
// Function to check whether a
// number is power of 2 or not
static long ispowerof2 ( long num )
{
    if ((num & (num - 1)) == 0)
        return 1;
    return 0;
}
 
// Driver code
public static void main(String[] args)
{
    long num = 549755813888L;
    System.out.println(ispowerof2(num));
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 program to find whether a number
# is power of 2 or not
 
# Function to check whether a
# number is power of 2 or not
def ispowerof2(num):
 
    if((num & (num - 1)) == 0):
        return 1
    return 0
 
# Driver function
if __name__=='__main__':
    num = 549755813888
    print(ispowerof2(num))
     
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to find
// whether a number
// is power of 2 or not
class GFG{
// Function to check whether a
// number is power of 2 or not
static long ispowerof2 ( long num )
{
    if ((num&(num-1)) == 0)
        return 1;
    return 0;
}
 
// Driver Code
public static void Main()
{
long num = 549755813888;
System.Console.WriteLine(ispowerof2(num));
}
}
// This code is contributed
// by mits


PHP


输出:

0
0
1

时间复杂度: O(N ^ 2),其中N是给定数字中的位数。
方法2:使用boost库
Boost库旨在广泛有用,并且可以在广泛的应用程序中使用。例如,它们有助于处理C++中超出long long,long double数据类型(264)范围的大量数字。

  1. 将输入作为升压整数。
  2. 使用位操作检查其是否为2的幂。

C++

// C++ program to find whether a number
// is power of 2 or not
#include 
#include 
 
using namespace std;
using namespace boost::multiprecision;
 
// Function to check whether a
// number is power of 2 or not
bool ispowerof2 ( cpp_int num )
{
    if ( ( num & ( num - 1 ) ) == 0 )
        return 1;
    return 0;   
}
 
// Driver function
int main()
{
    cpp_int num = 549755813888;
    cout << ispowerof2 ( num ) << endl;
    return 0;
} 
 
// This code is contributed by  Aditya Gupta 4

Java

// Java program to find
// whether a number
// is power of 2 or not
class GfG
{
 
// Function to check whether a
// number is power of 2 or not
static long ispowerof2 ( long num )
{
    if ((num & (num - 1)) == 0)
        return 1;
    return 0;
}
 
// Driver code
public static void main(String[] args)
{
    long num = 549755813888L;
    System.out.println(ispowerof2(num));
}
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python3 program to find whether a number
# is power of 2 or not
 
# Function to check whether a
# number is power of 2 or not
def ispowerof2(num):
 
    if((num & (num - 1)) == 0):
        return 1
    return 0
 
# Driver function
if __name__=='__main__':
    num = 549755813888
    print(ispowerof2(num))
     
# This code is contributed by
# Sanjit_Prasad

C#

// C# program to find
// whether a number
// is power of 2 or not
class GFG{
// Function to check whether a
// number is power of 2 or not
static long ispowerof2 ( long num )
{
    if ((num&(num-1)) == 0)
        return 1;
    return 0;
}
 
// Driver Code
public static void Main()
{
long num = 549755813888;
System.Console.WriteLine(ispowerof2(num));
}
}
// This code is contributed
// by mits

的PHP


输出 :

1

时间复杂度: O(1)