Complement of a number with any base b | Function to find ( b - 1 ) 's complement ; Calculate number of digits in the given number ; Largest digit in the number system with base b ; Largest number in the number system with base b ; return Complement ; Function to find b 's complement ; b ' s ▁ complement ▁ = ▁ ( b - 1 ) ' s complement + 1 ; Driver code
function prevComplement ( n , b ) { var maxDigit , maxNum = 0 , digits = 0 , num = n ; while ( n != 0 ) { digits ++ ; n = parseInt ( n / 10 ) ; } maxDigit = b - 1 ; while ( ( digits -- ) > 0 ) { maxNum = maxNum * 10 + maxDigit ; } return maxNum - num ; } function complement ( n , b ) { return prevComplement ( n , b ) + 1 ; } document . write ( prevComplement ( 25 , 7 ) + " " ) ; document . write ( complement ( 25 , 7 ) ) ;