Skip to content

Instantly share code, notes, and snippets.

@alekfrohlich
Created August 26, 2020 12:38
Show Gist options
  • Save alekfrohlich/0914116bdb1a5710fcc5d5dabf719ac9 to your computer and use it in GitHub Desktop.
Save alekfrohlich/0914116bdb1a5710fcc5d5dabf719ac9 to your computer and use it in GitHub Desktop.
Gaussian integer multiplication, division, and greates common divisor.
"""Greatest common divisor for gaussian integers."""
def floor(x):
return x // 1
def mult_gi(x, y):
a, b = x
c, d = y
return (a*c - b*d, a*d + b*c)
def div_gi(x, y):
a, b = x
c, d = y
r = (a*c + b*d)/(c**2 + d**2)
s = (b*c - a*d)/(c**2 + d**2)
return (r, s)
def gcd_gi(x, y):
global COUNT
a, b = x
c, d = y
r, s = div_gi(x, y)
p = floor(r)
q = floor(s)
theta = (r-p, s-q)
gamma = mult_gi(theta, y)
if gamma == (0, 0):
return y
else:
return gcd_gi(y, gamma)
if __name__ == '__main__':
x = (85, 0)
y = (1, 13)
print(gcd_gi(x, y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment