Last active
April 24, 2016 11:49
-
-
Save black-black-cat/1f28acf19701b7e6ae83 to your computer and use it in GitHub Desktop.
计算n个数字的最大公约数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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