Last active
June 9, 2024 12:33
-
-
Save syrusakbary/7831365 to your computer and use it in GitHub Desktop.
Fibonacci algorithm in O(1) ;)
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
SQRT_5 = Math.sqrt(5) | |
GOLDEN_NUMBER = (1+SQRT_5)/2 | |
def fib(n) | |
(1/SQRT_5*(GOLDEN_NUMBER**n-(-1/GOLDEN_NUMBER)**n)).to_i | |
end | |
puts fib(7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment