'Intuitive Understanding of GCD algorithm
What's an intuitive way to understand how this algorithm finds the GCD?
function gcd(a, b) {
while (a != b)
if (a, b)
a -= b;
else
b -= a;
return a;
}
Solution 1:[1]
Wikipedia has a good article on it under the name Euclidean algorithm. In particular, this image from the article might answer your literal question: the intuitive way to understand how this algorithm finds GCD:
Subtraction-based animation of the Euclidean algorithm. The initial rectangle has dimensions a = 1071 and b = 462. Squares of size 462×462 are placed within it leaving a 462×147 rectangle. This rectangle is tiled with 147×147 squares until a 21×147 rectangle is left, which in turn is tiled with 21×21 squares, leaving no uncovered area. The smallest square size, 21, is the GCD of 1071 and 462.
Solution 2:[2]
The original inventor of the greatest common divisor algorithm was Euclid, who described it in his book Elements about 300 years before the birth of Christ. Here is his geometric explanation, including his diagram:

Let AB and CD be the two given numbers not relatively prime.
It is required to find the greatest common measure of AB and CD.
If now CD measures AB, since it also measures itself, then CD is a common measure of CD and AB. And it is clear that it is also the greatest, for no greater number than CD measures CD.
But, if CD does not measure AB, then, when the less of the numbers AB and CD being continually subtracted from the greater, some number is left which measures the one before it.
For a unit is not left, otherwise AB and CD would be relatively prime, which is contrary to the hypothesis.
Therefore some number is left which measures the one before it.
Now let CD, measuring BE, leave EA less than itself, let EA, measuring DF, leave FC less than itself, and let CF measure AE.
Since then, CF measures AE, and AE measures DF, therefore CF also measures DF. But it measures itself, therefore it also measures the whole CD.
But CD measures BE, therefore CF also measures BE. And it also measures EA, therefore it measures the whole BA.
But it also measures CD, therefore CF measures AB and CD. Therefore CF is a common measure of AB and CD.
I say next that it is also the greatest.
If CF is not the greatest common measure of AB and CD, then some number G, which is greater than CF, measures the numbers AB and CD.
Now, since G measures CD, and CD measures BE, therefore G also measures BE. But it also measures the whole BA, therefore it measures the remainder AE.
But AE measures DF, therefore G also measures DF. And it measures the whole DC, therefore it also measures the remainder CF, that is, the greater measures the less, which is impossible.
Therefore no number which is greater than CF measures the numbers AB and CD. Therefore CF is the greatest common measure of AB and CD.
Observe that Euclid uses the word "measures" to indicate that some multiple of a smaller length is the same as a larger length; that is, his concept "measures" is identical to our concept "divides" as in 7 divides 28.
Solution 3:[3]
In short, if both a and b is dividable by a D then it has to be a divisor of a-b and can not be bigger than a-b. The logic is to apply this recursively with the addition of the rule that for a=b the GCD is a:
GCD(a, b) = a == b ? a : GCD(min(a, b), abs(a-b))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amadan |
| Solution 2 | user448810 |
| Solution 3 | fejese |

