TheAlgorithms-C/project_euler/problem_5/sol.c

37 lines
579 B
C
Raw Normal View History

2019-10-01 22:21:55 +08:00
#include <stdio.h>
2020-04-08 21:41:12 +08:00
unsigned long gcd(unsigned long a, unsigned long b)
{
2019-10-01 22:21:55 +08:00
unsigned long r;
2020-04-08 21:41:12 +08:00
if (a > b)
{
2019-10-01 22:21:55 +08:00
unsigned long t = a;
a = b;
b = t;
}
2020-04-08 21:41:12 +08:00
while ((r = (a % b)))
{
2019-10-01 22:21:55 +08:00
a = b;
b = r;
}
return b;
}
2020-04-08 21:41:12 +08:00
unsigned long lcm(unsigned long a, unsigned long b)
{
2019-10-01 22:21:55 +08:00
unsigned long long p = (unsigned long long)a * b;
return p / gcd(a, b);
}
2020-04-08 21:41:12 +08:00
int main(void)
{
2019-10-01 22:21:55 +08:00
unsigned long ans = 1;
unsigned long i;
2020-04-08 21:41:12 +08:00
for (i = 1; i <= 20; i++)
{
2019-10-01 22:21:55 +08:00
ans = lcm(ans, i);
}
printf("%lu\n", ans);
return 0;
}