- Greatest Common Divisor, gcd(a, b)
- If b|a then gcd(a, b) = b.
- 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 */ 5 private static int gcd(int x, int y) { 6 if(y == 0) { 7 return x; 8 } 9 return gcd(y, x % y); 10 }
- 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.
- c