How to correctly calculate the coefficients of the Bezout ratio for the case of negative numbers in the implementation of the extended euclidean algorithm? Here is my code:
#include <iostream>
#include <cmath>
#include <tuple>
using namespace std;
tuple<int, int, int> xgcd(int a, int b, int s1 = 1, int s2 = 0, int t1 = 0, int t2 = 1) {
if (b == 0) {
return {abs(a), s1, t1};
}
int q = a / b;
return xgcd(b, a - q * b, s2, s1 - q * s2, t2, t1 - q * t2);
}
int main(double argc, char ** argv) {
tuple<int, int, int> result = xgcd(-10, -15);
cout << get<0>(result) << " " << get<1>(result) << " " << get<2>(result) << endl;
return 0;
};
In the presented case (-10, -15), the GCD is calculated correctly, but the Bezout coefficients require inverting the sign. What is the right way to deal with them? Thank you in advance!)
std::tuple(or pair) for code which is not a template, just declare a struct with proper field names. What is more readable and maintainable:get<0>(result)orresult.x?