I am trying to create a program where a user inputs two numbers - a base and an exponent, while exponent is a binary number. Then prints out the last two digits of the result. My code is working well, but according to the time execution it is not the fastest way to do it. I read a little about using Horner's method, is there a way to use it here? If so, how?
base = int(input())
exponent = int(input())
def binaryToDec(binary):
decimal, i = 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
return decimal
exponent = binaryToDec(exponent)
result = base ** exponent
result = abs(result)%100
print(result)
For example, the output of 3
and 1010
should be 49
.
Another example:
Input:
111111111111111111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111111111111111111
Output:
31
I expect the program to work faster in doing so, how can I do this?