-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgcd.c
44 lines (39 loc) · 951 Bytes
/
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
/*
* Author: Leandro Augusto Lacerda Campos <[email protected]>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 1: Algorithmic Toolbox
*
* Solution for Greatest Common Divisor Problem
*/
#include <stdio.h>
unsigned int gcd(unsigned int, unsigned int);
int main()
{
unsigned int a, b;
scanf("%u %u", &a, &b);
printf("%u\n", gcd(a, b));
return 0;
}
// gcd: calculate the greatest common divisor of two non-negative integers a and b,
// where 0 <= a, b <= 2 * 10^9. This function implements the Euclidean algorithm.
unsigned int gcd(unsigned int a, unsigned int b)
{
if (a > b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
else
{
if (a == 0)
return b;
else
return gcd(a, b % a);
}
}