Created
March 3, 2021 02:56
-
-
Save soffes/b6fc681d99dc55df55d7e9c0078dad8b to your computer and use it in GitHub Desktop.
Fibonacci without recursion
This file contains 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
# 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