Skip to content

Instantly share code, notes, and snippets.

@soffes
Created March 3, 2021 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soffes/b6fc681d99dc55df55d7e9c0078dad8b to your computer and use it in GitHub Desktop.
Save soffes/b6fc681d99dc55df55d7e9c0078dad8b to your computer and use it in GitHub Desktop.
Fibonacci without recursion
# From this delightful video https://www.youtube.com/watch?v=ghxQA3vvhsk
PHI = (1 + Math.sqrt(5)) / 2
def fib(n)
(((PHI**n) - ((-1 / PHI)**n)) / Math.sqrt(5)).to_i
end
(1...10).map { |n| fib(n) }
# => [1, 1, 2, 3, 5, 8, 13, 21, 34]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment