Skip to content

Instantly share code, notes, and snippets.

@mingtsay
Last active December 12, 2015 03:38
Show Gist options
  • Save mingtsay/4708640 to your computer and use it in GitHub Desktop.
Save mingtsay/4708640 to your computer and use it in GitHub Desktop.
GCD & LCM Reference
#include <stdio.h>
int gcd(int a, int b)
{
int m;
while(b)
{
m = b;
b = a % b;
a = m;
}
return a;
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
int main()
{
int n, i, l;
while(scanf("%d", &n) != EOF && n > 0) // 新測資
{
scanf("%d", &i); // 第一筆資料
l = i;
for(--n; n; --n) // 剩下的資料
{
scanf("%d", &i);
l = lcm(l, i);
}
printf("%d\n", l);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment