Wednesday, March 16, 2016

Code for GCD

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 gcd(int a , int b)
{
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

No comments:

Post a Comment

Speedup Android Studio

Go to Help ->Edit Custom VM Options, and chnge this 4 setting. After that close your Android Studio. This settings are for 8gb of ram pc...