Skip to content

Instantly share code, notes, and snippets.

@jacquerie
Created September 15, 2014 07:51
Show Gist options
  • Save jacquerie/dc88973a1efecc0b6159 to your computer and use it in GitHub Desktop.
Save jacquerie/dc88973a1efecc0b6159 to your computer and use it in GitHub Desktop.
A Fibonacci implementation that uses memoization.
this.fibonacci = (function (n) {
var fibo = {};
var rec = function (n) {
var tmp;
if (n in fibo) {
tmp = fibo[n];
} else if (n === 0 || n === 1) {
tmp = n;
} else {
tmp = rec(n-1) + rec(n-2);
}
fibo[n] = tmp;
return tmp;
};
return rec;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment