GCD GCD code, and thought

# Euclidean algorithm

Now, let's learn about Euclidean algorithm.

  • Euclidean algorithm known as Euclidean, mainly for operator greatest common divisor between two positive numbers. For the greatest common name, the English name (Greatest Common Divisor), we will use the following gcd represented on behalf of said common denominator.
  • Baidu is defined on Wikipedia: with a larger number by the smaller number, then the remainder appear (first remainder) to remove the divisor, the remainder then appear (second remainder) removing the remainder of the first, and so forth, until Finally, the remainder is 0 so far. If you are seeking the greatest common divisor of two numbers, the last two numbers of the divisor is the greatest common divisor.

Attach Code:

ll gcd(ll a, ll b)
{
    if (!b)
        return a;
    else
        return gcd(b, a % b);
}
//递归版本

ll gcd(ll a, ll b)
{
    ll t;
    while(b)
    {
        t=b;
        b=a%b;
        a=t;
    }
    return a;
}
//迭代版本

Guess you like

Origin www.cnblogs.com/Yunrui-blogs/p/11082243.html
gcd