Euclidean algorithm description

Euclidean algorithm description

The greatest common divisor of two numbers is divisible refers to their maximum positive integer simultaneously.
Provided two atoms a, b (a≥b), the step of seeking the greatest common divisor of a and b (a, b) as follows:
(1) with a by b (a≥b), to give a / b = p. ..r1 (R1> = 0);
(2) if r1 = 0, then (a, b) = r1; ! if r1 = 0, then b is divided by r1, obtain b / r1 = q ... R2 (R2> = 0);
(. 3) If r2 = 0 ,, then (a, b) = r1; ! If r2 = 0, r1 by dividing continue R2, ......, it goes on, until divisible so far.
Its last remainder is the divisor (a, b) of the greatest common divisor of zero.

Code:

int gcd (int a,int b)  //默认a>b
{
    if(b == 0)
        return a;
    else
        return gcd(b,a%b);
}

————————————————

Original link: https://blog.csdn.net/Tong_zhi/article/details/79807115

Pseudocode:
1. If a <b, then the switch A, b
2. find r = a / b remainder
3.1 if r is 0 (the remainder is 0), then b is sought, and ends.
3.2 otherwise letting a = b, b = r, repeat steps 2

Guess you like

Origin www.cnblogs.com/zja2019/p/11825870.html