Check if a number N can be expressed in base B | Function to check if a number N can be expressed in base B ; Check if n is greater than 0 ; Initialize a boolean variable ; Check if digit is 0 or 1 ; Subtract the appropriate power of B from it and increment higher digit ; Given number N and base B ; Function call
function check ( n , w ) { var a = Array ( 105 ) . fill ( 0 ) ; var p = 0 ; while ( n > 0 ) { a [ p ++ ] = n % w ; n /= w ; n = parseInt ( n ) ; } var flag = true ; for ( i = 0 ; i <= 100 ; i ++ ) { if ( a [ i ] == 0 a [ i ] == 1 ) continue ; else if ( a [ i ] == w a [ i ] == w - 1 ) a [ i + 1 ] ++ ; else flag = false ; } return flag ; } var B = 3 , N = 7 ; if ( check ( N , B ) ) document . write ( " " ) ; else document . write ( " " ) ;