Skip to content

Instantly share code, notes, and snippets.

@nicholasaiello
Created October 3, 2017 21:17
Show Gist options
  • Save nicholasaiello/4b79a4178b42b1dcf239d8f5a99a9245 to your computer and use it in GitHub Desktop.
Save nicholasaiello/4b79a4178b42b1dcf239d8f5a99a9245 to your computer and use it in GitHub Desktop.
Memoized Fibonacci
const fibo = (() => {
let memo = {};
const _fibo = (n) => {
console.time();
let value;
if (n in memo) {
value = memo[n];
} else {
if (n <= 1) {
value = 1;
} else {
value = _fibo(n - 1) + _fibo(n - 2);
}
memo[n] = value;
}
console.timeEnd();
return value;
};
return _fibo;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment