For example, if A = 864927518, B = 1462579282 ,M = 193773611, how to compute (A^B)%M?
Is there a simple way?
For example, if A = 864927518, B = 1462579282 ,M = 193773611, how to compute (A^B)%M?
Is there a simple way?
Yes: use modular exponentiation. Python's built in pow function allows you to do this with its optional third argument:
>>> pow(A, B, M)
2767533
3>> pow(864927518, 1462579282, 193773611)
2767533
I shall blithely assume that you are using ^ as the exponentiation operator here (rather than as XOR). You can either use the built-in pow or you can code your own:
import math
def expmod(i, j, m):
"""Compute (i ** j) % m"""
acc = 1
mask = 2 ** int(math.ceil(math.log(j, 2)))
while mask > 0:
acc = acc * acc
if mask & j:
acc = acc * i
j = j ^ mask
mask = mask // 2
acc = acc % m
return acc