Created
November 18, 2015 11:26
-
-
Save simonlindholm/5fce72c3f8583d719da9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 parallel_invert(l, n): | |
'''Inverts all elements of a list modulo some number, using 3(n-1) modular multiplications and one inversion.''' | |
culm = l[:] | |
for i in xrange(len(l)-1): | |
culm[i+1] = (culm[i] * l[i+1]) % n | |
try: | |
inv = invert(culm[-1], n) | |
except ZeroDivisionError: | |
return gcd(culm[-1], n) | |
for i in xrange(len(l)-1, 0, -1): | |
culm[i] = (inv * culm[i-1]) % n | |
inv = (inv * l[i]) % n | |
culm[0] = inv | |
return culm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment