Last active
August 29, 2015 14:11
-
-
Save ssanin82/3dd88441e3f681a353b6 to your computer and use it in GitHub Desktop.
Python: Fast power of b modulo x
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 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