Skip to content

Instantly share code, notes, and snippets.

@jremmen
Last active December 18, 2015 05: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 jremmen/5734004 to your computer and use it in GitHub Desktop.
Save jremmen/5734004 to your computer and use it in GitHub Desktop.
js: recursive sum, avg, gcd, and lcm
function sum(n) {
return n.length ? n[0] + sum(n.slice(1)) : 0;
}
function avg(n) {
return sum(n) / n.length;
}
function gcd(n1, n2) {
return n1 % n2 === 0 ? n2 : gcd(n2 % n1, n1);
}
function lcm(n1, n2) {
return (n1 * n2) / gcd(n1, n2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment