-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuclid.java
More file actions
45 lines (37 loc) · 1.2 KB
/
Euclid.java
File metadata and controls
45 lines (37 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/******************************************************************************
* Compilation: javac Euclid.java
* Execution: java Euclid p q
*
* Reads two command-line arguments p and q and computes the greatest
* common divisor of p and q using Euclid's algorithm.
*
* Remarks
* -----------
* - may return the negative of the gcd if either p or q is negative
*
******************************************************************************/
public class Euclid {
// recursive implementation
public static int gcd(int p, int q) {
if (q == 0) return p;
else return gcd(q, p % q);
}
// non-recursive implementation
public static int gcd2(int p, int q) {
while (q != 0) {
int temp = q;
q = p % q;
p = temp;
}
return p;
}
// main method
public static void main(String[] args) {
int p = Integer.parseInt(args[0]);
int q = Integer.parseInt(args[1]);
int d = gcd(p, q); //resursion
int d2 = gcd2(p, q); //while loop
System.out.println("gcd(" + p + ", " + q + ") = " + d);
System.out.println("gcd(" + p + ", " + q + ") = " + d2);
}
}