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/GCD (greatest common diviso...

30 lines
760 B
JavaScript

function gcd_more_than_two_numbers(input) {
if (toString.call(input) !== "[object Array]")
return false;
var len, a, b;
len = input.length;
if ( !len ) {
return null;
}
a = input[ 0 ];
for ( var i = 1; i < len; i++ ) {
b = input[ i ];
a = gcd_two_numbers( a, b );
}
return a;
}
function gcd_two_numbers(x, y) {
if ((typeof x !== 'number') || (typeof y !== 'number'))
return false;
x = Math.abs(x);
y = Math.abs(y);
while(y) {
var t = y;
y = x % y;
x = t;
}
return x;
}
console.log(gcd_more_than_two_numbers([3,15,27]));
console.log(gcd_more_than_two_numbers([5,10,15,25]));