[math]Greatest common divisor

Greatest common divisor

Euclid's algorithm: gcd

gcd( a, 0 ) = a
gcd( a, b ) = gcd( b, a mod b )

<!-- lang: cpp -->
unsigned int gcd( unsigned int M, unsigned int N )
{
    unsigned int Rem;
    while ( N > 0 ) {
        Rem = M % N;
        M = N;
        N = Rem;
    }

    return Rem;
}

你可能感兴趣的:([math]Greatest common divisor)