-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuclidAlgorithm.cpp
More file actions
49 lines (37 loc) · 1.14 KB
/
euclidAlgorithm.cpp
File metadata and controls
49 lines (37 loc) · 1.14 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
44
45
46
47
48
49
/**
* @file euclidAlgorithm.cpp
* @brief Receives two numbers (argv[1], argv[2]) to compute their GCD/GCF/HCF.
* @author Pedro Henrique Pinto de Oliveira
* @date 2024-07-19
*/
/* Inclusions */
#include "headers/errors.h"
#include "headers/xor_swap.h"
#include <iostream>
#include <cmath>
using namespace std;
/* Constants */
/* Types */
/* Functions */
int euclidAlgo(long a, long b)
{
if(b == 0) return a;
return euclidAlgo(b, a % b);
}
long euclidCalcGDC(long a, long b)
{
long GDC = 1;
if(abs(a) < abs(b)) xorSwap(&a, &b); // can't divide a integer by a greater integer and get quotient > 0
if(a == b) return a; // mdc(a, a) = ax + ay (Bézout) = a^2(x+y) | a \forall a \therefore (a, a) = a.
GDC = euclidAlgo(abs(a), abs(b)); // absolute value needed for recursive implementation
return GDC;
}
int main(int argc, char * argv[])
{
long a, b;
if(argc != NO_ARGS_PROVIDED + 2) return NOT_ENOUGH_ARGS;
a = strtol(argv[1], nullptr, 0);
b = strtol(*(&argv[1] + sizeof(char)), nullptr, 0);
cout << euclidCalcGDC(a, b) << '\n';
return SUCCESS;
}