Yet Another one on Fibonacci serie - Knuth multiplication

Fibonacci serie - Knuth multiplication - Java

Leonardo Fibonacci,the Italian mathematician (from Pisa, 1170, one of the most important mathematician of all time) invented the famous serie defined recursively as follows:

    \[F(n) = F(n-1) + F(n-2)\]

    \[F(0) = 0\]

    \[F(1) = 1\]

We all know that as programmers once in life (at least) we will write some piece of code to compute the n_{th} element of the serie. Recursive implementation is straightforward but inefficient, due to the exponential number of recursive calls, that make "hard" to compute numbers bigger that 40. It is very easy to come up with some iterative or memoised implementation, that lowers the complexity to O(n) and allows to efficently compute "any" number of the serie.
In this post we will derive an implementation (JAVA) that have complexity O(log_2(n))1)using results from Knuth, Fibonacci multiplication,1988.

Knuth observed that the following holds:

    \[\left(\begin{array}{ccc} F_{n+1} & F_{n}\\ F_{n} & F_{n-1} \end{array}\right)\]


    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)^n\]

infact it easy to proof by induction:

Base case: N=1

    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)^1\]


    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)\]


    \[\left(\begin{array}{ccc} F_2 & F_1\\ F_1 & F_0 \end{array}\right)\]

Suppose it hold up to n and show that holds for n+1

    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)^{n+1}\]


    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)\]

    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)^{n}\]


    \[\left(\begin{array}{ccc} 1 & 1\\ 1 & 0 \end{array}\right)\]

    \[\left(\begin{array}{ccc} F_{n+1} & F_{n}\\ F_{n} & F_{n-1} \end{array}\right)\]


    \[\left(\begin{array}{ccc} F_{n+1}+F_{n} & F_{n}+F_{n-1}\\ F_{n+1} & F_{n} \end{array}\right)\]


    \[\left(\begin{array}{ccc} F_{n+2} & F_{n+1}\\ F_{n+1} & F_{n} \end{array}\right)\]

Another observation that we'll use to derive the algorithm is that if A is a matrix then A^n = A^l A^m:l+m=n, and in particular with n even A^n = A^{\frac{n}{2}} A^{\frac{n}{2}}

Let's see the actual code.

The FibMatr class, simply is used to store the 4 integer field used during the Knuth multiplication. BigInteger are required here due to the large number that the serie produces very soon. multiply method performs the matrix multiplication and store the result in a new matrix leaving the operands unaffected by the operation.

class FibMatr{
	BigInteger Q0,Q1,Q2,Q3;

	public  FibMatr(BigInteger a,BigInteger b,BigInteger c,BigInteger d) {

	public  FibMatr() {

	public FibMatr multiply(FibMatr p){
		FibMatr f= new FibMatr();
		return f;

This is the recursive function used to compute the n^{th} element of the serie. Base case return the Knuth matrix while if the current n is even we recursively find the matrix at \frac{n}{2} and the return it squared. If n is odd the Knuth matrix is multiplied with the n-1th element. The overall complexity is obviously O(log_2(n))

public static  FibMatr fibLog(int n){
		return (new FibMatr());
		if( (n & 0x1) ==0){
			FibMatr m = fibLog( n/2);
			return m.multiply(m);
			FibMatr m = new FibMatr();
			return m.multiply(fibLog( n-1 ) );


References   [ + ]

1. using results from Knuth, Fibonacci multiplication,1988

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>