Created
July 28, 2020 20:03
-
-
Save bdsaglam/41beb8fd52d667b32a100e54692f2e39 to your computer and use it in GitHub Desktop.
Pulverizer algorithm to find smallest linear combination of two positive numbers
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 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