-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy patheuclidean.cpp
More file actions
39 lines (31 loc) · 733 Bytes
/
euclidean.cpp
File metadata and controls
39 lines (31 loc) · 733 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
#include <cmath>
#include <iostream>
#include <utility>
// Euclidean algorithm using modulus
int euclid_mod(int a, int b) {
a = std::abs(a);
b = std::abs(b);
while (b != 0) {
a = std::exchange(b, a % b);
}
return a;
}
// Euclidean algorithm with subtraction
int euclid_sub(int a, int b) {
a = std::abs(a);
b = std::abs(b);
while (a != b) {
if (a > b) {
a -= b;
} else {
b -= a;
}
}
return a;
}
int main() {
auto check1 = euclid_mod(64 * 67, 64 * 81);
auto check2 = euclid_sub(128 * 12, 128 * 77);
std::cout << "[#]\nModulus-based euclidean algorithm result:\n" << check1 << '\n';
std::cout << "[#]\nSubtraction-based euclidean algorithm result:\n" << check2 << '\n';
}