-
Notifications
You must be signed in to change notification settings - Fork 8
/
lcm.c
52 lines (46 loc) · 1.17 KB
/
lcm.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
50
51
52
/*
* Author: Leandro Augusto Lacerda Campos <llacerdacampos@gmail.com>
*
* 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 Least Common Multiple Problem
*/
#include <stdio.h>
unsigned long long lcm(unsigned int, unsigned int);
unsigned int gcd(unsigned int, unsigned int);
int main()
{
unsigned int a, b;
scanf("%u %u", &a, &b);
printf("%llu\n", lcm(a, b));
return 0;
}
// lcm: calculate the least common multiple of two positive integers a and b,
// where 1 <= a, b <= 2 * 10^9.
unsigned long long lcm(unsigned int a, unsigned int b)
{
return (unsigned long long)a * b / gcd(a, b);
}
// gcd: calculate the greatest common divisor of two non-negative integers a and b.
// 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);
}
}