Euclid.java
Below is the syntax highlighted version of Euclid.java
from §2.7 Recursion.
/*************************************************************************
* Compilation: javac Euclid.java
* Execution: java Euclid p q
*
* Reads two commandline parameters p and q and computes the greatest
* common divisor of p and q using Euclid's algorithm.
*
*************************************************************************/
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.parseInt(args[0]);
int q = Integer.parseInt(args[1]);
int d = gcd(p, q);
System.out.println("gcd(" + p + ", " + q + ") = " + d);
}
}
Last updated: Mon Oct 4 13:16:43 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.