Created
September 15, 2014 07:51
-
-
Save jacquerie/dc88973a1efecc0b6159 to your computer and use it in GitHub Desktop.
A Fibonacci implementation that uses memoization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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