Skip to content

Instantly share code, notes, and snippets.

@Jaliborc
Created April 30, 2012 15:09
Show Gist options
  • Save Jaliborc/2559099 to your computer and use it in GitHub Desktop.
Save Jaliborc/2559099 to your computer and use it in GitHub Desktop.
Exponentiation by squaring
def square(x): return x * x
def mod_exp(a , b, q):
print(b)
if b == 0: return 1
if b % 2 == 0: return square(mod_exp(a , b / 2, q))
return a * mod_exp(a , b - 1, q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment