Created
April 27, 2020 09:34
-
-
Save ellcs/3384a369b1e3b2efe05bc5879437cbfd to your computer and use it in GitHub Desktop.
Fast fibbonacci
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
# https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression | |
N = (1 + Math::sqrt(5)) / 2 | |
M = (1 - Math::sqrt(5)) / 2 | |
def fibb(n) | |
(N**n - M**n) / (N - M) | |
end | |
# https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form | |
require 'matrix' | |
def matrix_fibb(n) | |
Matrix[[1, 0]] * Matrix[[0,1], [1,1]]**(n) * Matrix[[0], [1]] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment