Last active
December 26, 2015 10:09
-
-
Save yangjunjun/7134519 to your computer and use it in GitHub Desktop.
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
//Iterative | |
function factorial(num) | |
{ | |
var rval=1; | |
for (var i = 2; i <= num; i++) | |
rval = rval * i; | |
return rval; | |
} |
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
//memoization | |
var f = []; | |
function factorial (n) { | |
if (n == 0 || n == 1) | |
return 1; | |
if (f[n] > 0) | |
return f[n]; | |
else | |
return f[n] = factorial(n-1) * n; | |
} |
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
//Recursive | |
function factorial(num) | |
{ | |
if (num === 0) | |
{ return 1; } | |
else | |
{ return num * factorial( num - 1 ); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment