Skip to content

Instantly share code, notes, and snippets.

@YanivHaramati
Created January 11, 2015 07:14
Show Gist options
  • Save YanivHaramati/bc38ea8671034fbb7bcb to your computer and use it in GitHub Desktop.
Save YanivHaramati/bc38ea8671034fbb7bcb to your computer and use it in GitHub Desktop.
dp factorial as map reduce in javascript.
function factorial(num) {
if (num <= 2) return num;
return Array.apply(null, Array(num))
.map(function (_, i) { return i + 1; })
.reduce(function(p, c, i, a) {
a[i] = (i < 2) ? i+1 : (i + 1) * a[i-1];
return a[i];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment