Euclidean algorithm algorithm _

Greatest common divisor of two numbers, such as 50 and 15 is the greatest common divisor of 5,

Algorithm is as follows:

# Euclid greatest common divisor

def gcd(m, n):
    while (n != 0):
        rem = m % n
        m = n
        n = rem

    return m;


# Test 

A = GCD (50, 15 )
 Print (A)

 

Assuming m> n, the first cycle, m replacing n, n is a remainder REM replaced, the cycle continues until the remainder is 0, returns the greatest common divisor .

 

Guess you like

Origin www.cnblogs.com/coloz/p/11015812.html