Skip to content

Instantly share code, notes, and snippets.

@desigens
Created June 30, 2018 10:35
Show Gist options
  • Save desigens/f44833a4700fe79370ab20e0e896212f to your computer and use it in GitHub Desktop.
Save desigens/f44833a4700fe79370ab20e0e896212f to your computer and use it in GitHub Desktop.
Codility
var yourself = {
cache: {},
fibonacci : function(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
let a = this.cache[n - 1];
if (a === undefined) {
a = this.fibonacci(n - 1);
this.cache[n - 1] = a;
}
let b = this.cache[n - 2];
if (b === undefined) {
b = this.fibonacci(n - 2);
this.cache[n - 2] = b;
}
return a + b;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment