Greatest common divisor

The greatest common divisor of two numbers, $$x$$ and $$y$$ is the biggest integer that divides both $$x$$ and $$y$$.

This comes in handy when calculating the least common multiple, since $$lcm(a,b) = \frac{ a b }{ gcd(a,b) }$$.

The gcd can be found by using the Euclidean algorithm:

func gcd( var a as Integer, var b as Integer ) if ( b > 0 ) return gcd( b, a mod b ) return a