-
Notifications
You must be signed in to change notification settings - Fork 302
/
euclidean_gcd.c
49 lines (43 loc) · 980 Bytes
/
euclidean_gcd.c
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
#include <stdio.h>
/*
Calculates GCD of two numbers a & b using the division-based Euclidean Algorithm
*/
int gcd_div(int a , int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
/*
Calculates GCD of two numbers a & b using the recursive-based Euclidean Algorithm
*/
int gcd_rec(int a, int b) {
if (a == 0)
return b;
return gcd_rec(b % a, a);
}
/*
Calculates GCD of two numbers a & b using the Recursive-based Extended Euclidean Algorithm
*/
int gcd_extended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int result = gcd_extended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return result;
}
// Main Method
int main() {
int a = 20, b = 30, x, y;
printf("Division: GCD(%d, %d) = %d\n", a, b, gcd_div(a, b));
printf("Recursive: GCD(%d, %d) = %d\n", a, b, gcd_rec(a, b));
int gcd_ext = gcd_extended(a, b, &x, &y);
printf("Extended: GCD(%d, %d) = %d\n", a, b, gcd_ext);
}