Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
Created July 28, 2020 20:03
Show Gist options
  • Save bdsaglam/41beb8fd52d667b32a100e54692f2e39 to your computer and use it in GitHub Desktop.
Save bdsaglam/41beb8fd52d667b32a100e54692f2e39 to your computer and use it in GitHub Desktop.
Pulverizer algorithm to find smallest linear combination of two positive numbers
def quotient_remainder(a, b):
# returns quotient and remainder of a division b
# a = q*b + r
q = 0
while a >= b:
q += 1
a -= b
return q, a
def pulverizer(a, b):
u0, v0 = 0, 1
u1, v1 = None, None
while b > 0:
q, r = quotient_remainder(a, b)
a, b = b, r
if u1 is None:
u1, v1 = 1, -q
else:
u = -q * u1 + u0
v = -q * v1 + v0
u0, v0 = u1, v1
u1, v1 = u, v
return a, u0, v0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment