Check if a large number is divisible by a number which is a power of 2
Last Updated :
11 May, 2021
Given a large number in the form of a string str and a number K, the task is to check if the number formed by string str is divisible by the K or not, where K is a power of 2.
Examples:
Input: str = “5426987513245621541524288”, num = 64
Output: Yes
Explanation:
Since log2(64) = 6, so the number formed by the last 6 digits from the string str is divisible by 64 .
Input: str = “21346775656413259795656497974113461254”, num = 4
Output: No
Explanation:
Since log2(4)=2, the number formed by the last 2 digits from the string str is not divisible by 4.
Approach:
Since K is a perfect power of 2. Let K can be represented as 2X. Then according to the divisibility rule of perfect power of 2, if the last X digit of the given number is divisible by K then the given number is divisible by K. Otherwise it is not divisible by K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkIfDivisible(string str,
long long int num)
{
long long int powerOf2 = log2(num);
if (str.length() < powerOf2)
return false ;
if (powerOf2 == 0)
return true ;
long long int i, number = 0;
int len = str.length();
for (i = len - powerOf2; i < len; i++) {
number += (str[i] - '0' )
* pow (10,
powerOf2 - 1);
powerOf2--;
}
if (number % num)
return false ;
else
return true ;
}
int main()
{
string str = "213467756564" ;
long long int num = 4;
if (checkIfDivisible(str, num))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean checkIfDivisible(String str,
long num)
{
long powerOf2 = ( int )(Math.log(num) /
Math.log( 2 ));
if (str.length() < powerOf2)
return false ;
if (powerOf2 == 0 )
return true ;
long i, number = 0 ;
int len = str.length();
for (i = len - powerOf2; i < len; i++)
{
number += (str.charAt(( int )i) - '0' ) *
Math.pow( 10 , powerOf2 - 1 );
powerOf2--;
}
if (number % num != 0 )
return false ;
else
return true ;
}
public static void main(String[] args)
{
String str = "213467756564" ;
long num = 4 ;
if (checkIfDivisible(str, num))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
from math import log2
def checkIfDivisible(string, num):
powerOf2 = int (log2(num));
if ( len (string) < powerOf2):
return False ;
if (powerOf2 = = 0 ):
return True ;
number = 0 ;
length = len (string);
for i in range (length - powerOf2, length):
number + = (( ord (string[i]) - ord ( '0' )) *
( 10 * * (powerOf2 - 1 )));
powerOf2 - = 1 ;
if (number % num):
return False ;
else :
return True ;
if __name__ = = "__main__" :
string = "213467756564" ;
num = 4 ;
if (checkIfDivisible(string, num)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool checkIfDivisible(String str,
long num)
{
long powerOf2 = ( int )(Math.Log(num) /
Math.Log(2));
if (str.Length < powerOf2)
return false ;
if (powerOf2 == 0)
return true ;
long i, number = 0;
int len = str.Length;
for (i = len - powerOf2; i < len; i++)
{
number += ( long )((str[( int )i] - '0' ) *
Math.Pow(10, powerOf2 - 1));
powerOf2--;
}
if (number % num != 0)
return false ;
else
return true ;
}
public static void Main(String[] args)
{
String str = "213467756564" ;
long num = 4;
if (checkIfDivisible(str, num))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkIfDivisible(str, num)
{
let powerOf2 = (Math.log(num) /
Math.log(2));
if (str.length < powerOf2)
return false ;
if (powerOf2 == 0)
return true ;
let i, number = 0;
let len = str.length;
for (i = len - powerOf2; i < len; i++)
{
number += (str[i] - '0' ) *
Math.pow(10, powerOf2 - 1);
powerOf2--;
}
if (number % num != 0)
return false ;
else
return true ;
}
let str = "213467756564" ;
let num = 4;
if (checkIfDivisible(str, num))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(Len), where Len is the length of the string.
Auxiliary Space: O(log2K)
Share your thoughts in the comments
Please Login to comment...