Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Last active December 26, 2015 10:09
Show Gist options
  • Save yangjunjun/7134519 to your computer and use it in GitHub Desktop.
Save yangjunjun/7134519 to your computer and use it in GitHub Desktop.
//Iterative
function factorial(num)
{
var rval=1;
for (var i = 2; i <= num; i++)
rval = rval * i;
return rval;
}
//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;
} ​
//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