I have a question, how does
pow(num, power, mod)
work so much faster than
(num**power)%mod
On large numbers the second is unusable, so i'm wondering how pow() works. How does it work to be so much faster, what is the underpinning of how it works with mod to calculate an answer so much faster than
(num**power)%mod.
(num * * power)%mod is unusable with larger numbers
so i'm wondering what trick pow() uses to calculate the answer so quickly? Does it mod first, then power? Hopefully somebody can help me understand how this works.
import time
shared_Prime = 1031267
base_prime = 111029
secret_num = 103123
start = time.time()
A = (secret_num**base_prime) % shared_Prime
end = time.time()
print(end - start)
0.1082313060760498
start = time.time()
B = pow(secret_num, base_prime, shared_Prime)
end = time.time()
print(end - start)
8.916854858398438e-05
A==B
True