Skip to content

Instantly share code, notes, and snippets.

@teppeis
Created March 29, 2015 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teppeis/35eccdc8660b51b5ed53 to your computer and use it in GitHub Desktop.
Save teppeis/35eccdc8660b51b5ed53 to your computer and use it in GitHub Desktop.
// Options: --proper-tail-calls
"use strict";
var cache = {};
function fib(n) {
return fib_(n, _ => _);
}
function fib_(n, callback) {
if (n === 0) return callback(0);
if (n === 1) return callback(1);
if (cache[n]) return callback(cache[n]);
return fib_(n - 1, p1 => fib_(n - 2, p2 => callback(cache[n] = p1 + p2)));
}
console.log(fib(10000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment