Created
October 13, 2014 14:09
-
-
Save pingjiang/1eee54b8a610fa7dfde8 to your computer and use it in GitHub Desktop.
better memoized fabnacci
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
function fab(n) { | |
if (n < 3) { | |
return 1; | |
} | |
var p1=1, p2=1, p; | |
function _fab(m) { | |
if (m < n) { | |
p = p1 + p2; | |
p1 = p2; | |
p2 = p; | |
return _fab(m+1); | |
} | |
return p1+p2; | |
} | |
return _fab(3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment