Created
January 11, 2015 07:14
-
-
Save YanivHaramati/bc38ea8671034fbb7bcb to your computer and use it in GitHub Desktop.
dp factorial as map reduce 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
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