Skip to content

Instantly share code, notes, and snippets.

@vigack
Created August 2, 2017 08:21
Show Gist options
  • Save vigack/dcc36a8140f0a7af03629dc528863771 to your computer and use it in GitHub Desktop.
Save vigack/dcc36a8140f0a7af03629dc528863771 to your computer and use it in GitHub Desktop.
最大公因数、最小公倍数相关
int gcd(int a, int b){
int rmd; // remainder
if(a<b) // insure a>b
return gcd(b,a);
while((rmd=a%b)!=0){
a = b;
b = rmd;
} // loop down, b is the gcd
return b;
}
int lcm(int a, int b){
return a*b/gcd(a,b);
}
int multiGcd(int size, int *arr){
int mg = arr[0];
for(int i=1;i<size;i++)
mg = gcd(mg, arr[i]);
return mg;
}
int multiLcm(int size, int *arr){
int ml = arr[0];
for(int i=1;i<size;i++)
ml = lcm(ml, arr[i]);
return ml;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment