Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active December 20, 2015 00:48
Show Gist options
  • Select an option

  • Save rfaisal/6043920 to your computer and use it in GitHub Desktop.

Select an option

Save rfaisal/6043920 to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<stdio.h>
long int gcd(long int a, long int b){
if(b==0) return a;
else return gcd(b,a%b);
}
long int lcm(long int a, long int b){
return a*b/gcd(a,b);
}
long int lcm_arr(long int *arr, int n){
if(n==1) return arr[0];
else{
arr[n-2]=lcm(arr[n-1],arr[n-2]);
return lcm_arr(arr,n-1);
}
}
/**
* USAGE: ./a.out 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
**/
int main(int argc, char *argv[]){
if(argc<2) return 1;
long int *arr=malloc((argc-2)*sizeof(long int));
for(int i=1;i<argc;i++){
*(arr+i-1) = atoi(argv[i]);
}
printf("lcm=%ld",lcm_arr(arr,argc-1));
free(arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment