forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuclideanAlgo.java
34 lines (29 loc) · 884 Bytes
/
EuclideanAlgo.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
// submitted by lolatomroflsinnlos, modified by xam4lor
public class EuclideanAlgo {
public static int euclidSub(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
public static int euclidMod(int a, int b) {
while (b != 0) {
int tmp = b;
b = a % b;
a = tmp;
}
return a;
}
public static void main(String[] args) {
System.out.println("[#]\nModulus-based euclidean algorithm result:");
System.out.println(euclidMod(64 * 67, 64 * 81));
System.out.println("[#]\nSubtraction-based euclidean algorithm result:");
System.out.println(euclidSub(128 * 12, 128 * 77));
}
}