200 likes | 358 Vues
This Java program demonstrates the implementation of Euclid's algorithm to compute the greatest common divisor (GCD) of two integers. The algorithm uses recursion to efficiently find the GCD by repeatedly applying the modulo operation until one of the numbers becomes zero. The main method takes two integer inputs from the command line, calls the GCD function, and prints the result. This example highlights the simplicity and elegance of recursive functions in programming.
E N D
Recursive GCD Demo publicclass Euclid { public staticint gcd(int p,int q) { if(q ==0)return p; elsereturn gcd(q, p % q); } publicstaticvoid main(String[] args) { int p = Integer.parseInt(args[0]); int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); } }
static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment
static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment
static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment
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 p = 1272, q = 216 environment environment 1272 = 216 5 + 192
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 = 1272, q = 216 p = 216, q = 192 environment environment
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 = 1272, q = 216 p = 216, q = 192 environment environment
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 = 216, q = 192 p = 1272, q = 216 environment environment p = 192, q = 24 environment
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 = 216, q = 192 p = 1272, q = 216 environment environment p = 192, q = 24 environment
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 = 216, q = 192 p = 1272, q = 216 environment environment p = 192, q = 24 environment
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) static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(24, 0) p = 216, q = 192 p = 1272, q = 216 environment 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(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); } gcd(24, 0) p = 216, q = 192 p = 1272, q = 216 environment 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); } 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); } gcd(1272, 216) gcd(216, 192) p = 1272, q = 216 p = 216, q = 192 environment environment p = 192, q = 24 environment p = 24, q = 0 24 environment
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 = 216, q = 192 p = 1272, q = 216 environment environment p = 192, q = 24 environment 24
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 = 216, q = 192 p = 1272, q = 216 environment environment 24 p = 192, q = 24 environment 24
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 p = 1272, q = 216 environment environment 24
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 p = 1272, q = 216 environment environment 24 24
static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment 24
static int gcd(int p, int q){ if (q ==0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment 24 publicclass Euclid { public staticint gcd(int p,int q) { if(q ==0)return p; elsereturn gcd(q, p % q); } publicstaticvoid main(String[] args) { int p = Integer.parseInt(args[0]); int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); } } 24 24 % java Euclid 1272 216 24