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
< ? php function prevComplement ( $ n , $ b ) { $ maxNum = 0 ; $ digits = 0 ; $ num = $ n ; while ( ( int ) $ n != 0 ) { $ digits ++ ; $ n = $ n / 10 ; } $ maxDigit = $ b - 1 ; while ( $ digits -- ) { $ maxNum = $ maxNum * 10 + $ maxDigit ; } return $ maxNum - $ num ; } function complement ( $ n , $ b ) { return prevComplement ( $ n , $ b ) + 1 ; } echo prevComplement ( 25 , 7 ) , " STRNEWLINE " ; echo ( complement ( 25 , 7 ) ) ; ? >