Finding the GCD of three numbers?
As others say, one way to do it is using the identity $\gcd(a,b,c)=\gcd(a,(\gcd(b,c))$. This identity is true since the "gcd" is the maximal element of the intersection of the sets of factors of the inputs. For example, taking $\gcd(6,10)$, the set of factors of $6$ is {$6, 3, 2, 1$}, the set of factors of $10$ is {$10, 5, 2, 1$}, their intersection is {$2, 1$}, and the maximal element is $2$. The identity follows since intersections of sets are commutative and associative.
However, it's also possible to extend Stein's algorithm to handle more than two inputs. Wikipedia gives 5 steps to Stein's algorithm, and we can change them to the following to compute the gcd of a set $S$ of numbers:
- If anything in $S$ is zero, remove it.
- If everything in $S$ is even, divide everything in $S$ by 2, and then multiply the final answer by 2 at the end.
- If some numbers in $S$ are even and some are odd, divide all the even numbers by 2.
- If every number in $S$ is odd, then choose an arbitrary element of $S$ and call it $k$. Replace every other element, say $n$, with $|n-k|/2$.
- Repeat steps 1–4 until there is only one remaining element in $S$. After you remember to multiply in the twos that you got from step 2, that's your gcd!