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
def prevComplement ( n , b ) : NEW_LINE INDENT maxNum , digits , num = 0 , 0 , n NEW_LINE while n > 1 : NEW_LINE INDENT digits += 1 NEW_LINE n = n // 10 NEW_LINE DEDENT maxDigit = b - 1 NEW_LINE while digits : NEW_LINE INDENT maxNum = maxNum * 10 + maxDigit NEW_LINE digits -= 1 NEW_LINE DEDENT return maxNum - num NEW_LINE DEDENT def complement ( n , b ) : NEW_LINE INDENT return prevComplement ( n , b ) + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE