A common (but slow and confusing) way of calculating the greatest common divisor is using an iterative algorithm to solve. Something like this:

An ancient greek mathematician came up with a much more elegant way of calculating GCD. The algorithm, known as Euclid's algorithm, can be implemented using recursion. Here is my implementation (it is quite simple):