Skip to content

Instantly share code, notes, and snippets.

@mrandrewmills
Created May 1, 2016 17:27
Show Gist options
  • Save mrandrewmills/59af444dc4a63fb3e4a305382e1748c1 to your computer and use it in GitHub Desktop.
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.
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;
}
}
@mrandrewmills
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment