Skip to content

Instantly share code, notes, and snippets.

@viewv
Created November 25, 2022 08:45
Show Gist options
  • Save viewv/7da0adb80d563615e1c64156b96a144b to your computer and use it in GitHub Desktop.
Save viewv/7da0adb80d563615e1c64156b96a144b to your computer and use it in GitHub Desktop.
Exponentiating by squaring with mod
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