Skip to content

Instantly share code, notes, and snippets.

@donaldevine
Last active September 1, 2017 15:12
Show Gist options
  • Save donaldevine/a8b146def5c4f70018418a2195f1060f to your computer and use it in GitHub Desktop.
Save donaldevine/a8b146def5c4f70018418a2195f1060f to your computer and use it in GitHub Desktop.
Fibonacci with Memoization O(2N)
var calc = {
fibonacci : function(n, memo) {
memo = memo || {};
if (memo[n]) return memo[n];
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
memo[n] = this.fibonacci(n - 1, memo) +
this.fibonacci(n - 2, memo);
return memo[n];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment