You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-examples/js/Math/Calculate the extended Eucl...

40 lines
1015 B
JavaScript

function Euclid_gcd(a, b) {
a = +a;
b = +b;
if (a !== a || b !== b) {
return [NaN, NaN, NaN];
}
if (a === Infinity || a === -Infinity || b === Infinity || b === -Infinity) {
return [Infinity, Infinity, Infinity];
}
// Checks if a or b are decimals
if ((a % 1 !== 0) || (b % 1 !== 0)) {
return false;
}
var signX = (a < 0) ? -1 : 1,
signY = (b < 0) ? -1 : 1,
x = 0,
y = 1,
u = 1,
v = 0,
q, r, m, n;
a = Math.abs(a);
b = Math.abs(b);
while (a !== 0) {
q = Math.floor(b / a);
r = b % a;
m = x - u * q;
n = y - v * q;
b = a;
a = r;
x = u;
y = v;
u = m;
v = n;
}
return [b, signX * x, signY * y];
}
console.log(Euclid_gcd(17, 4));