Skip to content

Instantly share code, notes, and snippets.

@idmontie
Created February 13, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idmontie/5ac681b819877123afec to your computer and use it in GitHub Desktop.
Save idmontie/5ac681b819877123afec to your computer and use it in GitHub Desktop.
Greatest Common Divisor in JavaScript
/*
* Uses the Euvlidean Algorithm.
* http://en.wikipedia.org/wiki/Euclidean_algorithm
*/
function greatestCommonDivisor( a, b ) {
if ( b > a ) {
var t = a;
a = b;
b = t;
}
var difference = a - b;
if ( difference == 0 ) {
return a;
} else return greatestCommonDivisor( difference, b );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment