import java.util.Random; public class GCDTest { public static Random rand = new Random(); /** * requires: - 'a' >= 0 and 'b' >= 0 * ensures: - the result 'r' divides 'a' and 'b', i.e. a % r == 0 and * b % r == 0 * - There exists no 'v' > 'r' such that 'v' divides 'a' and 'b'. **/ public static int euclid(int a, int b) { if (b == 0) return a; return euclid(b, a % b); } /** * requires: - 'a' > 0 and 'b' > 0 * ensures: - the result 'r' divides 'a' and 'b', i.e. a % r == 0 and * b % r == 0 * - There exists no 'v' > 'r' such that 'v' divides 'a' and 'b'. **/ public static int binary(int a, int b) { int d = 0; while (a % 2 == 0 || b % 2 == 0) { a = a / 2; b = b / 2; d++; } while (a != b) { if (a % 2 == 0) { a = a / 2; } else if (b % 2 == 0) { b = b / 2; } else if (a > b) { a = (a - b) / 2; } else { b = (b - a) / 2; } } while (d != 0) { a = a * 2; d = d - 1; } return a; } public static int randomValue() { // TODO return 0; } public static boolean test(int a, int b) { try { // TODO return false; } catch (Exception e) { e.printStackTrace(); return false; } } public static void main(String[] arg) { for (int i = 0; i < 10000; i++) { int a = randomValue(); int b = randomValue(); if (!test(a, b)) { System.out.println("a: " + a); System.out.println("b: " + b); System.exit(0); } } System.out.println("Done."); } }