Skip to content

Instantly share code, notes, and snippets.

@abdalimran
Created February 12, 2016 19:05
Show Gist options
  • Save abdalimran/62cf11025074537a5d53 to your computer and use it in GitHub Desktop.
Save abdalimran/62cf11025074537a5d53 to your computer and use it in GitHub Desktop.
/* Bismillah hir rahmanir raheem. Thanks to Allah for everything.
Coder: Abdullah Al Imran
Email: [email protected] */
#include<bits/stdc++.h>
using namespace std;
//Normal Algorithm
int lcm(int a, int b)
{
for (int i=1; ;i++)
{
if(i%a==0 && i%b==0)
return i;
}
}
//Using GCD formula
int lcm (int a, int b)
{
return (a/gcd(a,b))*b;
}
//LCM of multiple numbers in an array
int lcm_m(int *num)
{
int lcm;
lcm=num[1]*num[2]/gcd(num[1],num[2]);
for(int i=3;i<=n;i++) //n=size of the array
lcm=lcm*num[i]/gcd(lcm,num[i]);
return lcm;
}
int main()
{
ios_base::sync_with_stdio(false);
cout<<lcm(8,12)<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment