Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
function countSetBits ( x ) { let count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } function ceilLog2 ( x ) { let count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } function isBleak ( n ) { for ( let x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } if ( isBleak ( 3 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( isBleak ( 4 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;