Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,11 +1,17 @@
function gcd(a,b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b > a) {var temp = a; a = b; b = temp;}
while (true) {
a %= b;
if (a == 0) return b;
b %= a;
if (b == 0) return a;
}
a = Math.abs(a);
b = Math.abs(b);
if (b > a) {
var temp = a;
a = b;
b = temp;
}
while (true) {
a %= b;
if (a === 0) { return b; }
b %= a;
if (b === 0) { return a; }
}
}

View file

@ -1,7 +1,3 @@
function gcd_rec(a, b) {
if (b) {
return gcd_rec(b, a % b);
} else {
return Math.abs(a);
}
return b ? gcd_rec(b, a % b) : Math.abs(a);
}

View file

@ -1,14 +1,18 @@
function GCD(A) // A is an integer array (e.g. [57,0,-45,-18,90,447])
{
var n = A.length, x = A[0] < 0 ? -A[0] : A[0];
for (var i = 1; i < n; i++)
{ var y = A[i] < 0 ? -A[i] : A[i];
while (x && y){ x > y ? x %= y : y %= x; }
x += y;
}
return x;
function GCD(arr) {
var i, y,
n = arr.length,
x = Math.abs(arr[0]);
for (i = 1; i < n; i++) {
y = Math.abs(arr[i]);
while (x && y) {
(x > y) ? x %= y : y %= x;
}
x += y;
}
return x;
}
/* For example:
GCD([57,0,-45,-18,90,447]) -> 3
*/
//For example:
GCD([57,0,-45,-18,90,447]); //=> 3