Thursday, September 1, 2011

Fibonacci (log n)

you have done the fibonacci series with 
1) recursion with O(n-1)+O(n);
2) linear dynamic programming c=a+b; a=b; b=c; in looping to n with O(n);			            
3) with matrix exponential O(log n);
so we are intesesting how the order log n is justified with matrix exponential;
here are the observations comeoncodeon-fr0ddy related to it i concluded that
  1. if we represent the fibonacci as the matrix than here first element of fibonacci series is 1 the matrix fib[0][0]
  2. if we have to find out fibonacci series at n then fib[]n
  3. n can be odd or even.
  4. if it is even n= 2 * k "k" be any integer then the matrix next element is found by repeated squaring i.e.
  5. else multiply by its previous matrix i.e. fib[]n=fib[] * fib[]n-1
  6. and loop till goal state is achieved
here is the code given explore it
you can also test its speed by testing cases at fibonacci(log n) code
see how fast it is running from your recursive fibonacci.

3 comments:

  1. Don't exaagerate. It is perfectly understandable. Even Americans speak a dialect intelligible to the British (English, Scottish and Irish).

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Complexity of recursive solution (without memoization) is exponential, not "O(n-1) + O(n)" which would be O(n).

    ReplyDelete