Wednesday, 2 October 2013

Euclid's Algorithm Theory

Euclid's Algorithm Theory

I understand the following code always produces the GCF of two integers. I
can see that variable b passes its value to a, and variable r passes its
value to variable b. This is according to Euclid's algorithm. However, I
don't understand why this works. Namely, how does the remainder share a
common factor with a and b?
// x and y are integers; x >= y > 0
a = x;
b = y;
r = a % b;
while (r > 0}
{
a = b;
b = r;
r = a % b;
}
// b is the greatest common factor of x and y

No comments:

Post a Comment