Created
November 11, 2014 05:32
-
-
Save danrpts/9e2ab357b2ea5e2c2ba5 to your computer and use it in GitHub Desktop.
A space-saving, bottom-up algorithm to calculate the nth Fibonacci number in linear time.
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
def fibonacci(n) | |
arr = [0, 1] | |
for k in 2..n do | |
arr[1] = arr[1] + arr.shift | |
end | |
return arr[1] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment