Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active April 24, 2016 11:49
Show Gist options
  • Save black-black-cat/1f28acf19701b7e6ae83 to your computer and use it in GitHub Desktop.
Save black-black-cat/1f28acf19701b7e6ae83 to your computer and use it in GitHub Desktop.
计算n个数字的最大公约数
// common divisor
function maxComDivisor() {
var args = [].slice.call(arguments);
var len = args.length;
function maxTwoDivi() {
// var args = [].slice.call(arguments);
// console.log(args);
var max = Math.max.apply(Math, arguments);
var min = Math.min.apply(Math, arguments);
if (max % min === 0) {
return min;
}
return maxTwoDivi(min, max % min);
}
if (len < 1) return;
if (len === 1) return args[0];
if (len === 2) {
return maxTwoDivi.apply(null, args);
}
return maxComDivisor(maxComDivisor.apply( null, args.slice(1) ), args[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment