public class GreatestCommonDivisor
1: //GreatestCommonDivisor.java
2:
3: public class GreatestCommonDivisor
4: {
5: public static void main(String[] args)
6: {
7: System.out.println("\nTesting gcd() ...");
8: System.out.println(gcd(12, 30));
9: System.out.println(gcd(30, 12));
10: System.out.println(gcd(12345, 54321));
11: System.out.println(gcd(30, 0));
12: System.out.println(gcd(0, 30));
13: System.out.println(gcd(34, 187));
14: System.out.println(gcd(7777, 555));
15: System.out.println(gcd(97, 43));
16: }
17:
18: //Computes and returns the greatest common divisor of a and b.
19: //Assumes a, b >= 0 and at least one of them is > 0.
20: public static int gcd
21: (
22: int a,
23: int b
24: )
25: {
26: if (b == 0)
27: return a;
28: else
29: return gcd(b, a % b);
30: }
31: }
32: