Created
February 13, 2015 16:39
-
-
Save idmontie/5ac681b819877123afec to your computer and use it in GitHub Desktop.
Greatest Common Divisor in JavaScript
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
/* | |
* 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