Modulus of two Hexadecimal Numbers | Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
def hexaModK ( s , k ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT mp [ chr ( i + ord ( '0' ) ) ] = i ; NEW_LINE DEDENT mp [ ' A ' ] = 10 ; NEW_LINE mp [ ' B ' ] = 11 ; NEW_LINE mp [ ' C ' ] = 12 ; NEW_LINE mp [ ' D ' ] = 13 ; NEW_LINE mp [ ' E ' ] = 14 ; NEW_LINE mp [ ' F ' ] = 15 ; NEW_LINE m = int ( k ) ; NEW_LINE base = 1 ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT n = mp [ s [ i ] ] % m ; NEW_LINE ans = ( ans + ( base % m * n % m ) % m ) % m ; NEW_LINE base = ( base % m * 16 % m ) % m ; NEW_LINE DEDENT ans = hex ( int ( ans ) ) [ - 1 ] . upper ( ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = "3E8" ; NEW_LINE k = "13" ; NEW_LINE hexaModK ( n , k ) ; NEW_LINE DEDENT