forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuclidean_algorithm.d
More file actions
42 lines (34 loc) · 738 Bytes
/
euclidean_algorithm.d
File metadata and controls
42 lines (34 loc) · 738 Bytes
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
import std.stdio;
import std.math;
// Euclidean algorithm using modulus
int euclid_mod(int a, int b) {
int tmp;
a = abs(a);
b = abs(b);
while (b != 0) {
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
// Euclidean algorithm with subtraction
int euclid_sub(int a, int b) {
a = abs(a);
b = abs(b);
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
void main()
{
auto check1 = euclid_mod(64 * 67, 64 * 81);
auto check2 = euclid_sub(128 * 12, 128 * 77);
writeln("Modulus-based euclidean algorithm result: ", check1);
writeln("Subtraction-based euclidean algorithm result: ", check2);
}