Created
May 1, 2016 17:27
-
-
Save mrandrewmills/59af444dc4a63fb3e4a305382e1748c1 to your computer and use it in GitHub Desktop.
a little JavaScript kata inspired by Dr. Marcus du Sautoy's discussion of Euclid's "Greatest Common Divisor" algorithm.
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
var Euclid = {}; | |
Euclid.gcd = function greatestCommonDivisor( a, b) { | |
"use strict"; | |
var lilNum, bigNum, modulo; | |
lilNum = Math.min( a, b); | |
bigNum = Math.max( a, b); | |
modulo = bigNum % lilNum; | |
if (modulo !== 0) { | |
return this.gcd(modulo, lilNum); | |
} | |
else { | |
return lilNum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't like recursive functions. I always seem to find a way to "blow the stack" when I write them, but this one went surprisingly well.