Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fibonacci using matrix multiplication #10

Open
enedil opened this issue Apr 10, 2017 · 4 comments
Open

Fibonacci using matrix multiplication #10

enedil opened this issue Apr 10, 2017 · 4 comments

Comments

@enedil
Copy link

enedil commented Apr 10, 2017

Hello.
I'm not fluent in Java so I won't implement it by myself, however there is an O(log n) solution for finding nth Fibonacci number which is based on matrix exponentiation -

[[1 1] [1 0]]^n = [[F_{n+1} F_n] [F_n F_{n-1}]]

This can be done with exponentiation by squaring which is O(log n).

@sherxon
Copy link
Owner

sherxon commented Apr 10, 2017

hi Thank you, i will add solution using this approach later

@cktang88
Copy link

cktang88 commented Aug 5, 2017

There's also a constant O(1) closed-form formula for the Nth fibonacci number - Binet's Formula

@enedil
Copy link
Author

enedil commented Aug 6, 2017

@cktang88 uhmm, it's not? There's exponentiation here which uses at least O(log n) multiplications. Moreover, these are floating point numbers, which are probably slower to operate on than four integers.

@cktang88
Copy link

cktang88 commented Aug 6, 2017

@enedil yes, you're correct. The matrix exponentiation would be more efficient from a CS point of view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants