shortcut for gcd / built in function in c++ for gcd :
int a,b,c;
cin>>a>>b;
c= __gcd(a,b); // here c is the gcd of a,b.
//Bismillah-hir-rah-ma-nir-rahim(in the name of Allah)
//Thinker: Arafat Kamal Tamzid(Sylhet Engineering College)
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
int t;
while(b!=0)
{
t=b;
b=a%b;
a=t;
}
return a;
}
int main()
{
int a,b,c;
cin>>a>>b;
c=gcd(a,b);
cout<<c<<endl;
return 0;
}
more faster function :
int a,b,c;
cin>>a>>b;
c= __gcd(a,b); // here c is the gcd of a,b.
//Bismillah-hir-rah-ma-nir-rahim(in the name of Allah)
//Thinker: Arafat Kamal Tamzid(Sylhet Engineering College)
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
int t;
while(b!=0)
{
t=b;
b=a%b;
a=t;
}
return a;
}
int main()
{
int a,b,c;
cin>>a>>b;
c=gcd(a,b);
cout<<c<<endl;
return 0;
}
more faster function :
int gcd(int a , int b)
{
if(b==0) return a;
a%=b;
return gcd(b,a);
}
No comments:
Post a Comment