Skip to content

Instantly share code, notes, and snippets.

@danrpts
Created November 11, 2014 05:32
Show Gist options
  • Save danrpts/9e2ab357b2ea5e2c2ba5 to your computer and use it in GitHub Desktop.
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.
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