Skip to content

Instantly share code, notes, and snippets.

@JiLiZART
Created June 26, 2016 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JiLiZART/6d0e26cd785f5efd95e4337849968737 to your computer and use it in GitHub Desktop.
Save JiLiZART/6d0e26cd785f5efd95e4337849968737 to your computer and use it in GitHub Desktop.
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