-
Notifications
You must be signed in to change notification settings - Fork 2
/
GCDTest.java
70 lines (57 loc) Β· 1.51 KB
/
GCDTest.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package GCD;
public class GCDTest {
public static void main(String[] args) {
System.out.println("[Get GCD By Euclidean algorithm]");
System.out.println(gcd(132, 36));
System.out.println(gcd(36, 132));
System.out.println("[Extended Euclidean algorithm]");
System.out.println(eGcd(9, 5));
}
static int gcd(int a, int b) {
// System.out.println("=========================");
// System.out.println("gcd(" + a + ", " + b + ")");
while (b != 0) {
int r = a % b;
a = b;
b = r;
// System.out.println("gcd(" + a + ", " + b + ")");
}
return a;
}
static ExtendedGCDResult eGcd(long a, long b) {
long s0 = 1, t0 = 0, r0 = a;
long s1 = 0, t1 = 1, r1 = b;
long temp;
while (r1 != 0) {
long q = r0 / r1;
temp = r0 - r1 * q;
r0 = r1;
r1 = temp;
temp = s0 - s1 * q;
s0 = s1;
s1 = temp;
temp = t0 - t1 * q;
t0 = t1;
t1 = temp;
}
return new ExtendedGCDResult(s0, t0, r0);
}
}
class ExtendedGCDResult {
long s;
long t;
long r;
public ExtendedGCDResult(long s, long t, long r) {
this.s = s;
this.t = t;
this.r = r;
}
@Override
public String toString() {
return "{" +
"s=" + s +
", t=" + t +
", r=" + r +
'}';
}
}