Recursive GCD Demo public class Euclid public static
Recursive GCD Demo public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } public static void main(String[] args) { int p = Integer. parse. Int(args[0]); int q = Integer. parse. Int(args[1]); System. out. println(gcd(p, q)); } } Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http: //www. cs. Princeton. EDU/Intro. CS
p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment 1272 = 216 5 + 192 p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 gcd(1272, 216) environment p = 216, q = 192 environment p = 192, q = 24 environment p = 24, q = 0 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(24, 0) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 gcd(1272, 216) environment p = 216, q = 192 environment p = 192, q = 24 environment p = 24, q = 0 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(24, 0) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 gcd(1272, 216) environment p = 216, q = 192 environment p = 192, q = 24 environment p = 24, q = 0 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 gcd(24, 0) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }
p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24
p = 1272, q = 216 environment p = 216, q = 192 environment p = 192, q = 24 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 gcd(192, 24) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24
p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24
p = 1272, q = 216 environment p = 216, q = 192 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24
p = 1272, q = 216 environment gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24
p = 1272, q = 216 gcd(1272, 216) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } 24 public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } } public static void main(String[] args) { int p = Integer. parse. Int(args[0]); 24 int q = Integer. parse. Int(args[1]); System. out. println(gcd(p, q)); } 24 % java Euclid 1272 216 24
- Slides: 19