Skip to content

Instantly share code, notes, and snippets.

@shohan4556
Created February 16, 2015 19:07
Show Gist options
  • Select an option

  • Save shohan4556/c224bc9bb17b9083129e to your computer and use it in GitHub Desktop.

Select an option

Save shohan4556/c224bc9bb17b9083129e to your computer and use it in GitHub Desktop.
extends euclid algo bimplement c++
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a, int b, int &x, int &y) {
if (a == 0) {
x = 0; y = 1;
return b;
}
int x1, y1;
int d = gcd(b%a, a, x1, y1);
x = y1 - (b/a) * x1;
y = x1;
return d;
}
int main(void) {
int a, b, x, y, d;
while (cin >> a >> b) {
d = gcd(a, b, x, y);
if (a == b) {
x = 0;
y = 1;
}
cout << x << " " << y << " " << d << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment