Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Need help with the fibonacci Q-Matrix
Author Message
wensi

Posted: Thu Aug 30, 2007 9:18 pm   Post subject: Need help with the fibonacci Q-Matrix

I was doing the online judge problems and accidentally found that there is an algorithm which finds the n'th fibonacci in O(log n). It uses the Q-Matrix property and the code is as following:

 code: Divide_Conquer_Fib(n) {   i = h = 1;   j = k = 0;   while (n > 0) {     if (n%2 == 1) { // if n is odd       t = j*h;       j = i*h + j*k + t;       i = i*k + t;     }     t = h*h;     h = 2*k*h + t;     k = k*k + t;     n = (int) n/2;   }   return j; }

I spent hours trying to figure out how it works but still havent find any clue. I know how the matrix works but I dont understand the variables and the arithmetic in the code since its so badly documented. Can anyone please explain to me? or can we discuss it a bit?

zylum

Posted: Thu Aug 30, 2007 11:32 pm   Post subject: RE:Need help with the fibonacci Q-Matrix

have you ever seen this formula? http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html I haven't read the problem yet but the formula may be a better solution.
Catalyst

Posted: Fri Aug 31, 2007 1:09 am   Post subject: Re: Need help with the fibonacci Q-Matrix

the code seems to be just a streamlined version of the Q matrix method using a binary decomposition for the powers.
So for Q^{19} = (Q^16)*(Q^2)*(Q^1) then it would square the Q matrix in each loop then if the binary decomp of n
had the particular bit in it, it would multiply the matrix (to hold Q^n) by the one its been squaring. That might be a little
vague, so heres a non-streamlined version of the above code:
 code: void mult(int& a1,int& b1,int& c1,int& d1,int a,int b,int c,int d) {         int w,x,y,z;         w=a1;x=b1;y=c1;z=d1;         a1=a*w+b*y;              b1=a*x+b*z;         c1=c*w+d*y;         d1=c*x+d*z;                } int fib_Q(int n) {         int a,b,c,d;         int w,x,y,z;                 a=b=c=1;         d=0;                 w=x=y=1;         z=0;         while (n>0) {                 if (n%2 == 1) {                         mult(a,b,c,d,w,x,y,z);                        }                 mult(w,x,y,z,w,x,y,z);                 n=(int)(n/2);               }         return b;               }

the code you posted also seems to take advantage of the fact that Q is symmetric, and rolls some calculations into one
wensi

Posted: Fri Aug 31, 2007 10:32 am   Post subject: Re: RE:Need help with the fibonacci Q-Matrix

zylum @ Thu Aug 30, 2007 11:32 pm wrote:
have you ever seen this formula? http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html I haven't read the problem yet but the formula may be a better solution.

that formula isnt accurate for large numbers since u cant get a very precise sqrt(5) using the cmath header.
wensi

Posted: Fri Aug 31, 2007 11:03 am   Post subject: Re: Need help with the fibonacci Q-Matrix

Catalyst @ Fri Aug 31, 2007 1:09 am wrote:

the code seems to be just a streamlined version of the Q matrix method using a binary decomposition for the powers.
So for Q^{19} = (Q^16)*(Q^2)*(Q^1) then it would square the Q matrix in each loop then if the binary decomp of n
had the particular bit in it, it would multiply the matrix (to hold Q^n) by the one its been squaring. That might be a little
vague, so heres a non-streamlined version of the above code:

thx for ur explaination and ur code, i will be very glad if u could elaborate how the streamlining and combination took place?
Catalyst

Posted: Fri Aug 31, 2007 2:56 pm   Post subject: Re: Need help with the fibonacci Q-Matrix

what they did is get rid of the mult function my code has and just put those operations right into the main loop. They also dont keep track of the lower
left entry in the Q matrix since it will be equal to the upper right (since the matrix is symmetric). And as a final trick they also dont store the upper left entry of the
Q-matrix but use the fact the a=b+d ( Qrow1=[a,b] Qrow2=[c,d]) (since the entries are fibonacci numbers F_n+1=Fn+F_n-1) to eliminate it from the calculations. After that they avoid doing repeated calculations by storing a few calculations common to more that one line of code in a dummy variable (t in their case).
wensi

Posted: Sat Sep 01, 2007 9:57 am   Post subject: RE:Need help with the fibonacci Q-Matrix

thank you so much, now i finally understand the code
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 7 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: