-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy patheuclidean_alg.c
More file actions
41 lines (37 loc) · 812 Bytes
/
euclidean_alg.c
File metadata and controls
41 lines (37 loc) · 812 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
// Two simple implementations of the Euclidean algorithm, both recursive and iterative
#include <stdio.h>
int recursive_euclides(int a, int b)
{
if (a == b)
{
return a;
} else if (a > b)
{
return recursive_euclides(a - b, b);
} else
{
return recursive_euclides(a, b - a);
}
}
int iter_euclides(int a, int b)
{
while (a != b)
{
if (a > b)
{
a = a - b;
} else if (a < b)
{
b = b - a;
}
}
return a;
}
int main(int argc, char const *argv[])
{
int a = 25;
int b = 20;
printf("The GCD of %d and %d is %d\t(recursive attempt)\n", a, b, recursive_euclides(a, b));
printf("The GCD of %d and %d is %d\t(iterative attempt)\n", a, b, iter_euclides(a, b));
return 0;
}