// This class computes Greatest Common Divisor using // Euclid's algorithm. // The two alternative functions use either recursion // or iteration. public class GCD{ public static final boolean GCD_REC = true; public static final boolean GCD_IT = false; public static long gcd(long a, long b, boolean method){ //Check the algorithm condition and perform //an eventual swap if(a < b){ long tmp = a; a = b; b = tmp; } if(method) return gcd_rec(a, b); else return gcd_it(a, b); } private static long gcd_rec(long a, long b){ if(a % b == 0) return b; return gcd_rec(b, a % b); } private static long gcd_it(long a, long b){ long tmp_a = 1; while(a % b != 0){ tmp_a = a % b; a = b; b = tmp_a; }; return tmp_a; } }