Created
August 31, 2011 21:21
-
-
Save pixeldrew/1184748 to your computer and use it in GitHub Desktop.
Math extensions
This file contains hidden or 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
// http://www.ideashower.com/our_solutions/leastgreatest-common-mulitple-lcmgcm-in-php-and-javascript/ | |
/* Greatest Common Divisor */ | |
Math.gcd = function(a, b) { | |
return ( b == 0 ) ? (a):( Math.gcd(b, a % b) ); | |
}; | |
/* Lowest Common Divisor */ | |
Math.lcd = function(a, b) { | |
return ( a / Math.gcd(a,b) ) * b; | |
}; | |
/* given an array find the lowest common divisor */ | |
Math.lcds = function (ar) { | |
if (ar.length > 1) { | |
ar.push( Math.lcd( ar.shift() , ar.shift() ) ); | |
return Math.lcds( ar ); | |
} else { | |
return ar[0]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment