Created
June 7, 2016 23:20
-
-
Save Istar-Eldritch/31ba354bedcb7b2156a2487667f5cfb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 trampoline(f) { | |
while (f && f instanceof Function) { | |
f = f.apply(f.context, f.args); | |
} | |
return f; | |
} | |
function factorial(n) { | |
function _factorial(n, acc) { | |
if(n) { | |
return _factorial.bind(null, n - 1, acc * n) | |
} else { | |
return acc; | |
} | |
} | |
return trampoline(_factorial.bind(null, n, 1)); | |
} | |
console.log(factorial(50000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment