Last active
December 26, 2015 05:29
-
-
Save gitawego/7100610 to your computer and use it in GitHub Desktop.
factorial (n!)
bench: http://jsperf.com/factorial-test-my/2
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 fact1(x) { | |
if(x<=1) return 1; | |
else return x*fact(x-1); | |
} | |
function fact2(x){ | |
var res=x; | |
while(--x){ | |
res *= x; | |
} | |
return res; | |
} | |
//best performance | |
function fac1(x) | |
{ | |
var r = 1; | |
for(var i=2;i<=x;i++) | |
{ | |
r *= i; | |
} | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment