Created
June 26, 2016 15:35
-
-
Save JiLiZART/6d0e26cd785f5efd95e4337849968737 to your computer and use it in GitHub Desktop.
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
var fPrev, fCurr; | |
function fib (n) { | |
var result; | |
if (n < 0) throw Error('invalid given number'); | |
if (n <= 1) { | |
return n; | |
} | |
fPrev = 0; | |
fCurr = 1; | |
for (var i = 2; i <= n; i++) { | |
var newCurr = fPrev + fCurr; | |
fPrev = fCurr; | |
fCurr = newCurr; | |
} | |
return fCurr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment