Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Euclidean Algorithm work?

I just found this algorithm to compute the greatest common divisor in my lecture notes:

public static int gcd( int a, int b ) {
    while (b != 0) {
        final int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

So r is the remainder when dividing b into a (get the mod). Then b is assigned to a, and the remainder is assigned to b, and a is returned. I can't for the life of my see how this works!

And then, apparently this algorithm doesn't work for all cases, and this one must then be used:

public static int gcd( int a, int b ) {
    final int gcd;
    if (b != 0) {
        final int q = a / b;
        final int r = a % b; // a == r + q * b AND r == a - q * b.
        gcd = gcd( b, r );
    } else {
        gcd = a;
    }
    return gcd;
}

I don't understand the reasoning behind this. I generally get recursion and am good at Java but this is eluding me. Help please?

like image 931
John Curtsy Avatar asked May 14 '11 23:05

John Curtsy


People also ask

What is the Euclidean algorithm formula?

If we examine the Euclidean Algorithm we can see that it makes use of the following properties: GCD(A,0) = A. GCD(0,B) = B. If A = B⋅Q + R and B≠0 then GCD(A,B) = GCD(B,R) where Q is an integer, R is an integer between 0 and B-1.

Does Euclids algorithm always work?

It always terminates because at each step one of the two arguments to gcd(⋅,⋅) gets smaller, and at the next step the other one gets smaller. You can't keep getting smaller positive integers forever; that is the "well ordering" of the natural numbers.

Why Euclidean algorithm works for GCD?

The Euclidean algorithm is designed to create smaller and smaller positive linear combinations of x and y. Since any set of positive integers has to have a smallest element, this algorithm eventually has to end. When it does (i.e., when the next step reaches 0), you've found your gcd.


1 Answers

The Wikipedia article contains an explanation, but it's not easy to find it immediately (also, procedure + proof don't always answer the question "why it works").

Basically it comes down to the fact that for two integers a, b (assuming a >= b), it is always possible to write a = bq + r where r < b.

If d=gcd(a,b) then we can write a=ds and b=dt. So we have ds = qdt + r. Since the left hand side is divisible by d, the right hand side must also be divisible by d. And since qdt is divisible by d, the conclusion is that r must also be divisible by d.

To summarise: we have a = bq + r where r < b and a, b and r are all divisible by gcd(a,b).

Since a >= b > r, we have two cases:

  1. If r = 0 then a = bq, and so b divides both b and a. Hence gcd(a,b)=b.
  2. Otherwise (r > 0), we can reduce the problem of finding gcd(a,b) to the problem of finding gcd(b,r) which is exactly the same number (as a, b and r are all divisible by d).

Why is this a reduction? Because r < b. So we are dealing with numbers that are definitely smaller. This means that we only have to apply this reduction a finite number of times before we reach r = 0.

Now, r = a % b which hopefully explains the code you have.

like image 165
Omri Barel Avatar answered Sep 26 '22 00:09

Omri Barel