Skip to content

Instantly share code, notes, and snippets.

@ssanin82
Last active August 29, 2015 14:11
Show Gist options
  • Save ssanin82/3dd88441e3f681a353b6 to your computer and use it in GitHub Desktop.
Save ssanin82/3dd88441e3f681a353b6 to your computer and use it in GitHub Desktop.
Python: Fast power of b modulo x
def fastmodpow(a, b, x):
poww = res = 1
while b:
if b & 1:
pw, i = a, 1
while i < poww:
pw *= pw
pw %= x
i *= 2
res *= pw
res %= x
poww *= 2
b /= 2
return res % x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment