Skip to content

Instantly share code, notes, and snippets.

@jkal
Created May 9, 2010 11:57
Show Gist options
  • Save jkal/395111 to your computer and use it in GitHub Desktop.
Save jkal/395111 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
def euclidExtended(a, b):
if b == 0:
return a, 1, 0
dd, xx, yy = euclidExtended(b, a % b)
d, x, y = dd, yy, xx - int(a / b) * yy
# print a, b, d, x, y
return d, x, y
def solveLinearModularEquation(a, b, n):
d, xx, yy = euclidExtended(a, n)
#print d, xx, yy
if (b % d == 0):
x0 = (xx * (b / d)) % n
# print x0
for i in xrange(0, d):
print (x0 + i * (n / d)) % n,
else:
print "No solution."
solveLinearModularEquation(39, 117, 143)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment