Skip to content

Commit

Permalink
Merge pull request rathoresrikant#274 from codetochangeMinds/master
Browse files Browse the repository at this point in the history
Added Extended Eulid Gcd algorithm
  • Loading branch information
Srikant Singh authored Oct 19, 2018
2 parents 57d1bdc + 9051899 commit cc9e04a
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Cryptography/extended_euclid_gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Extended Euclid Gcd is often useful in finding inverse of
// number mod n efficently. If gcd=1, then equation will turned to be
// 1=x*a+y*b, no taking modulo with b we get x as inverse of
// a mod b
#include <bits/stdc++.h>
using namespace std;

// (d,x,y) is tuple
struct gcd_tuple{
int d;
int x;
int y;
};

// evaluate gcd using Extended-Euclid-Gcd
// d=x*a+b*y
// d is gcd of a and b
// and is smallest number that can be expressed in this form.
// extended_euclid_gcd(a,b){
// (d,x,y)=extended_euclid_gcd(b,a%b);
// a=b*q+r;
// return (d,y,x-y*q);
// }
struct gcd_tuple extended_euclid_gcd(int a,int b)
{
struct gcd_tuple e_gcd;
if(b==0)
{
e_gcd.d=a;
e_gcd.x=1;
e_gcd.y=0;
return e_gcd;
}

e_gcd=extended_euclid_gcd(b,a%b);
int q=a/b;
int x=e_gcd.x;
int y=e_gcd.y;
e_gcd.x=y;
e_gcd.y=x-y*q;

return e_gcd;
}

// output format is d=a*x+b*y
int main(){

int a,b;
cin>>a>>b;
struct gcd_tuple e_gcd;
e_gcd=extended_euclid_gcd(a,b);
cout<<e_gcd.d<<"="<<a<<"*"<<e_gcd.x<<"+"<<b<<"*"<<e_gcd.y;
}

0 comments on commit cc9e04a

Please sign in to comment.