I was trying to implement a Miller-Rabin primality test, and was puzzled why it was taking so long (> 20 seconds) for midsize numbers (~7 digits). I eventually found the fol
The way x = a**d % n
is calculated is to raise a
to the d
power, then modulo that with n
. Firstly, if a
is large, this creates a huge number which is then truncated. However, x = pow(a, d, n)
is most likely optimized so that only the last n
digits are tracked, which are all that are required for calculating multiplication modulo a number.