Python implementation Euclidean algorithm

Euclidean algorithm goal is to find the greatest common divisor of two numbers.

Calculating two non-negative integers greatest common divisor of p and q: if q is 0, p is the greatest common divisor. Otherwise, p divided by q get the remainder r, p and q is the greatest common divisor greatest common divisor of q and r.

def euclid(p, q):
    if q == 0:
        return p
    r = p % q
    return euclid(q, r)

if __name__ == "__main__":
    print(euclid(512,1024))

 

Guess you like

Origin www.cnblogs.com/frisk/p/11707757.html