If a = bt + r, for integers t and r, then gcd(a, b) = gcd(b, r).
Indeed, every common divisor of a and b also divides r. Thus gcd(a, b) divides r. But, of course,gcd(a, b)|b. Therefore, gcd(a, b) is a common divisor of b and r and hence gcd(a, b) ≤ gcd(b, r). The reverse is also true because every divisor of b and r also divides a.
1/** 2 * Java method to calculate the greatest common divisor
3 * @return the gcd of two integer
4*/ 5privatestaticint gcd(int x, int y) {
6if(y == 0) {
7return x;
8 }
9return gcd(y, x % y);
10 }