Skip to content

Instantly share code, notes, and snippets.

@Istar-Eldritch
Created June 7, 2016 23:20
Show Gist options
  • Save Istar-Eldritch/31ba354bedcb7b2156a2487667f5cfb4 to your computer and use it in GitHub Desktop.
Save Istar-Eldritch/31ba354bedcb7b2156a2487667f5cfb4 to your computer and use it in GitHub Desktop.
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