I'm coming from a programming aspect of this issue. So in Scheme code
(define (gcd a b)
(if (= b 0)
a
(gcd b (modulo a b))))
works and uses recursion, i.e., the ...(gcd b (modulo a b)))) recursively calls the function gcd again (and again) until the condition b = 0 is met, to give the answer a. So to use this function (gcd 12 20) gives 4.
Now, if I do this for more than two numbers, say $2, 12, 20, 120$
(gcd 2 (gcd 21 (gcd (20 120))))
I get the right answer too, i.e., 2. Can someone tell me why this is working correctly from a math standpoint?
On Wikipedia's Euclidean Algorithm article, it says
The GCD of three or more numbers equals the product of the prime factors common to all the numbers,[11] but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.[12] For example,
gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b)
I'm guessing it has something to do with the commutable "product of all prime factors." But still, this is recursion inside of recursion pair-wise. Why does this work?
Essentially it does not matter how you do group and associate the numbers, as long as all the numbers come into play at some time.
It works because you can see the GCD as two separate processes, done in sequence.
In the first one you are looking for the prime numbers that divide all the numbers. The order or the grouping are irrelevant because you are looking for a global property, and if two numbers are divisible by, say, $3$ and $7$, you can replace them by $3\cdot 7$ and no information is lost.
In the second phase, if there are prime numbers that are in the factorization of all the numbers, you are looking for the minimum of the exponents.
So, if $2$ numbers are divisible by $7^6$ and $2$ are divisible by $7^4$ and one by $7^2$, your GCD will contain the factor $7^{\min(6,4,2)}=7^2$.
I think it is intuitive to realize that to compute the minimum among several numbers you can group them as you want.