19
Introduction to Computer Science Robert Sedgewick and Kevin Wayne http://www.cs.Princeton.EDU/IntroCS 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) { System.out.println(gcd(1272, 216)); } }

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive GCD Demo public class Euclid { public static

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • http://www.cs.Princeton.EDU/IntroCS

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) { System.out.println(gcd(1272, 216)); }}

Page 2: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

Page 3: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

Page 4: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

Page 5: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(216, 192)p = 216, q = 192

environment

p = 1272, q = 216

environment

1272 = 216 5 + 192

Page 6: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(216, 192)p = 216, q = 192

environment

Page 7: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(216, 192)p = 216, q = 192

environment

Page 8: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 9: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 10: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 11: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(24, 0)p = 24, q =

0environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 12: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(24, 0)p = 24, q =

0environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 13: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(24, 0)p = 24, q =

0environment

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

24

Page 14: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)environment

24

p = 192, q = 24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 15: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

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)p = 192, q =

24environment

24

p = 216, q = 192

environment

p = 1272, q = 216

environment

24

Page 16: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(216, 192)

24

p = 216, q = 192

environment

p = 1272, q = 216

environment

Page 17: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(216, 192)p = 216, q = 192

environment

24

p = 1272, q = 216

environment

24

Page 18: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)

24

p = 1272, q = 216

environment

Page 19: Introduction to Computer Science Robert Sedgewick and Kevin Wayne  Recursive GCD Demo public class Euclid { public static

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); }

gcd(1272, 216)p = 1272, q =

216environment

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) { System.out.println(gcd(1272, 216)); }}

24

24 % java Euclid24