Created
January 6, 2016 10:38
-
-
Save gurunars/a1c3cfe631f516ac2c16 to your computer and use it in GitHub Desktop.
Fibonacci - non recursive solution
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
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