Euclidean algorithm + Python Code

Algorithm theory

For a = b * q + c
is present (a, b) = (b , c)

Proof:
Order d = (a, b)
there d | a, d | b
a c = a - b * q
known d | c, i.e., d is the b, c male factor

So e = (b, c)
apparently d <= e
and e | b, e | c
of a = b * q + c
-known e | a, i.e. e is the a, b male factor
available d> = e

d = e, i.e. (a, b) = (b, c)

Python code

# encoding:utf-8


def myGCD(a, b):
    """
    a,b顺序无所谓
    """
    while b != 0:
        # print(a, b)
        a, b = b, a % b
    return a


# print(myGCD(12075, 4655))
print(myGCD(172, 46))

Guess you like

Origin www.cnblogs.com/kexve/p/11878136.html