Created
November 25, 2022 08:45
-
-
Save viewv/7da0adb80d563615e1c64156b96a144b to your computer and use it in GitHub Desktop.
Exponentiating by squaring with mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def exp_mod(base,n,mod): | |
ans = 1 | |
while n: | |
if n & 1: | |
ans = (ans * base) % mod | |
base = (base * base) % mod | |
n >>= 1 | |
return ans |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment