Find if a given number, num is power of 2 or not. More specifically, find if given number can be expressed as 2^k where k >= 1. Return 1 if the number is a power of 2 else return 0
- Number of digits of the given number i.e (num) can be greater than 100.
- There are no leading zeros before a non-zero number. E.g 0000128 is not in input./li>
Input : 3 Output : 0 2^0 = 1 where as k >= 1 Input : 128 Output : 1
Method 1 : Using strings
The idea is to repeatedly divide the large number (represented as a string) by 2. To perform division, we traverse digits from right to left. If last digit itself is not divisible by 2, we return 0. Otherwise we follow school method of division.
# 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
len_str = len(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’):
# 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):
# 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 [tabby title="C#"]
0 0 1
Time Complexity : O(N^2) where N is number of digits in given number.
Method 2 : Using boost library
Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. For example, they are helpful for handling large numbers having range beyond the long long, long double data type (264) in C++.
- Take input as a boost integer.
- Using bit manipulation check whether it’s power of 2 or not.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if given number is a power of d where d is a power of 2
- Check if a number can be expressed as power | Set 2 (Using Log)
- Check whether a given Number is Power-Isolated or not
- Check if a number can be expressed as x^y (x raised to power y)
- Check if a number is power of k using base changing method
- Check if a number is a power of another number
- Check if any permutation of N equals any power of K
- Highest power of a number that divides other number
- Find whether a given number is a power of 4 or not
- Sum of digits of a given number to a given power
- Power of a prime number ‘r’ in n!
- GCD of a number raised to some power and another number
- Number of digits in 2 raised to power n
- Number of digits in N factorial to the power N
- Finding power of prime number p in n!