function extEuclid(m, n) {
if (m < 1 || n < 1)
throw Error('m and n must be positive');
if (Math.floor(m) != m || Math.floor(n) != n)
throw Error('m and n must be integers');
let a = [1, 0];
let b = [0, 1];
let r = [n, m % n];
let q = [NaN, Math.floor(m / n)];
for (let i = 2; r[i-1] != 0; i++) {
a.push(a[i-2] - a[i-1] * q[i-1]);
b.push(b[i-2] - b[i-1] * q[i-1]);
r.push(r[i-2] % r[i-1]);
q.push(Math.floor(r[i-2] / r[i-1]));
}
return {
text: tex`GDC(${m}, ${n}) = ${a[a.length-1]}(${m}) + ${b[b.length-1]}(${n}) = ${r[r.length-2]}`,
m, n,
a: a[a.length-1],
b: b[b.length-1],
gcd: r[r.length-2],
};
}