-3

There's one question posted about Fibonacci Series, which I am much familiar with it. But there are multiple answers and linked questions to it. As I was digging through it with some interest, there's a solution which is linked to here

This algorithm solves the problem by O(log(n)) quite impressive. But I couldn't understand the logic and so called Matrix exponentiation [looked wiki, but unable to relate to it].

So kindly anyone can explain exactly how they achieved with more details and better explanation [if you can explain with code, prefer in Java, much helpful].

Thanks :)

Community
  • 1
  • 1
RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • I would suggest to comment before voting down and why? I would like to understand that algorithm, seeking help to make it clear what exactly done. – RaceBase Jul 22 '13 at 06:21
  • Didn't downvote, but it was possibly because the logic used is more on the math side. The code used on that page is just an implementation of the math logic. – Mark M Jul 22 '13 at 06:25
  • Do these help? [Fibonacci number, matrix form](http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form) and [Exponentiation by squaring](http://en.wikipedia.org/wiki/Exponentiation_by_squaring) – Blastfurnace Jul 22 '13 at 07:07
  • @Blastfurnace, seen those links, Bit hard in understanding how that logic is implemented there. May be I can ask in this way, how to solve Fibonacci series in O(log n) time? with Matrix or with something else. – RaceBase Jul 22 '13 at 07:10

1 Answers1

1

What you need to understand is the algorithm, not the implementation.

The first thing you need to understand is that this algorithm will not give you all the fibonacci numbers, only those with with a n that is a power of 2.

The second thing is that a multiplication of constantly sized matrices of course takes constant ( O(1) ) time.

The trick now is to correctly note that the n'th fibonacci number can be formed by n-times multiplication of the matrix described in your link, which i will call M.

You get the log complexity by now "reordering" the matrix operations from, for example M*(M*(M*M)) to (M*M)*(M*M). With each matrix squaring, you go to M^2n instead of M^n+1.

kutschkem
  • 7,826
  • 3
  • 21
  • 56