Skip to content

Instantly share code, notes, and snippets.

@gurunars
Created January 6, 2016 10:38
Show Gist options
  • Save gurunars/a1c3cfe631f516ac2c16 to your computer and use it in GitHub Desktop.
Save gurunars/a1c3cfe631f516ac2c16 to your computer and use it in GitHub Desktop.
Fibonacci - non recursive solution
fibonacci : function(n) {
// f(0) = 0
// f(1) = 1
// f(n) = f(n-1) + f(n-2)
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
var previous = 0;
var current = 1;
for (var i=2; i <=n; i++) {
new_current = previous + current;
previous = current;
current = new_current;
}
return current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment