Coursera's Algorithmic toolbox assignment solutions( Greatest Common Divisor, Least Common Multiple ) :
Problem 3: Greatest Common Divisor
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
int main()
{
int x,y;
cin>>x>>y;
cout<<gcd(x,y);
}
This code is simple. There's no need for any explanation.
Problem 4: Least Common Multiple
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
int main()
{
long long x,y;
cin>>x>>y;
cout<<(long long)((x*y)/gcd(x,y)); //since LCM = x*y/GCD(x,y)
}
This code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation, comment down below!
Comments
Post a Comment