Check if a large number is divisible by a number which is a power of 2 | Function to check divisibility ; Calculate the number of digits in num ; Check if the length of the string is less than the powerOf2 then return false ; Check if the powerOf2 is 0 that means the given number is 1 and as every number is divisible by 1 so return true ; Find the number which is formed by the last n digits of the string where n = powerOf2 ; Check if the number formed is divisible by input num or not ; Given number ; Function call
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 ] - ' ' ) * Math . pow ( 10 , powerOf2 - 1 ) ; powerOf2 -- ; } if ( number % num != 0 ) return false ; else return true ; } let str = " " ; let num = 4 ; if ( checkIfDivisible ( str , num ) ) document . write ( " " ) ; else document . write ( " " ) ;