Skip to content

Instantly share code, notes, and snippets.

@ramsays
Last active January 29, 2019 19:47
Show Gist options
  • Save ramsays/bfcc83ed5d5bf3e6cac911176f977a30 to your computer and use it in GitHub Desktop.
Save ramsays/bfcc83ed5d5bf3e6cac911176f977a30 to your computer and use it in GitHub Desktop.
GCD Javascript Algorithm
var a, b, u, g, x, y;
u = 1;
x = 0;
a = 527; // Set here
b = 1258; // Set here
g = a;
y = b;
if (b == 0) { // For part d
console.log("Division by zero error. Set b > 0.");
return;
}
do {
var t = g % y;
var q = (g-t) / y;
var s = u - q*x;
u = x;
g = y;
x = s;
y = t;
}
while (y > 0);
var v = (g-a*u)/b;
if (u < 0) { // for part e
u = u + b/g;
v = v - a/g;
}
console.log(g,u,v);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment