Euclidean algorithm

The Euclidean algorithm is an algorithm for finding the greatest common divisor of two integers.

Pseudocode
func gcd( Integer a, Integer b ) if ( b != 0 ) return gcd( b, a mod b ) return a

Explanation
Why does this work?

First, observe the following important fact: $$gcd(a,b)$$ divides $$ax+by$$ for all $$x$$ and $$y$$ (because it divides both $$ax$$ and $$by$$, it divides their sum).

So any number dividing both $$a$$ and $$b$$ will also divide $$a-kb$$ for any integer $$k$$. Conversely, any integer dividing both $$b$$ and $$a-kb$$ will also divide $$a$$, because $$a=kb+(a-kb)$$. Thus, the set of common divisors of $$a$$ and $$b$$ is the same as the set of common divisors of $$b$$ and $$a-kb$$. In particular, their greatest common divisor is the same.

As $$a\, \bmod b$$ is indeed a number of the form $$a-kb$$ for some $$k$$, our algorithm is justified.

The point of the algorithm is to continue this procedure until one number is 0, because $$gcd(0,x)=x$$, which we can then return as our answer.

Complexity
It can be proved that the number of steps taken by this algorithm is linear in the size of $$a$$ and $$b$$, which means that this is a very efficient algorithm.