Check if a given number can be represented in given a no . of digits in any base | Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver code
function checkUtil ( num , dig , base ) { if ( dig == 1 && num < base ) return true ; if ( dig > 1 && num >= base ) return checkUtil ( parseInt ( num / base ) , -- dig , base ) ; return false ; } function check ( num , dig ) { for ( base = 2 ; base <= 32 ; base ++ ) if ( checkUtil ( num , dig , base ) ) return true ; return false ; } var num = 8 ; var dig = 3 ; if ( check ( num , dig ) ) document . write ( " " ) ; else document . write ( " " ) ;