Skip to content

Instantly share code, notes, and snippets.

@pixeldrew
Created August 31, 2011 21:21
Show Gist options
  • Save pixeldrew/1184748 to your computer and use it in GitHub Desktop.
Save pixeldrew/1184748 to your computer and use it in GitHub Desktop.
Math extensions
// 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