Finding the nth fib number, in O(logn)

You mean the nth Fibonacci number I hope.

In order to do it you need a matrix decomposition of Fibonacci numbers described here.

The basic idea is you take the Donald E. Knuth matrix identity form for a Fibonacci number which is:

fib matrix equation

And instead of calculating the Fibonacci numbers in the traditional way you will try and find the matrix to the power of (k) where k is the given number.

So this is solving the problem in k matrix multiplications, not really helpful since we can do it in much easier way.

But wait! We can optimise the matrix multiplication. Instead of doing the k multiplications we can square it first and then do the half of the multiplications. And we can keep on doing it. So if the given number is 2a then we can do it in a steps. By keeping squaring the matrix.

If the number is not a power of 2 we can do the binary decomposition of a number and see whether to take the given squared matrix into final product or not.

In your case after each multiplication you also need to apply modulo operator 123456 to each matrix element.

Hope my explanation helps if not see the link for a clearer and longer one.

There is actually one more caveat of the task: as you are asked to provide some Fibonacci number modulo a given number, you should also prove that taking the remainder of each matrix element doesn't change the result. In other words if we multiply matrices and take remainder that we are actually still getting the Fibonacci number remainders. But since the remainder operation is distributive in addition and multiplication it actually does produce the correct results.


The Fibonacci numbers occur as the ratio of successive convergents of the continued fraction for , and the matrix formed from successive convergents of any continued fraction has a determinant of +1 or −1.

The matrix representation gives the following closed-form expression for the Fibonacci numbers i.e.

The matrix is multiplied n time because then only we can get the (n+1)th Fibonacci number as the element at the row and the column (0, 0) in the resultant matrix.

If we apply the above method without using recursive multiplication of matrix than the Time Complexity: O(n) and Space Complexity: O(1).

But we want Time Complexity: O(log n) so we have to optimize the above method and this can be done by recursive multiplication of matrix to get the nth power.

Implementation of the above rule can be found below.

#include <stdio.h>

void multiply(int F[2][2], int M[2][2]);

void power(int F[2][2], int n);

/*
The function that returns nth Fibonacci number.
*/

int fib(int n) {
    int F[2][2] = {{1, 1}, {1, 0}};
    if (n == 0)
        return 0;
    power(F, n - 1);
    return F[0][0];
}

/*
Optimized using recursive multiplication.
*/

void power(int F[2][2], int n) {
    if ( n == 0 || n == 1)
        return;
    int M[2][2] = {{1, 1}, {1, 0}};
    power(F, n / 2);
    multiply(F, F);
    if (n % 2 != 0)
        multiply(F, M);
}

void multiply(int F[2][2], int M[2][2]) {
    int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
    int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
    int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
    int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}

int main() {
    printf("%d\n", fib(15));
    /*
    15th Fibonacci number is 610.
    */
    return 0;
}

There is a very simple algorithm, using only integers:

long long fib(int n) {
    long long a, b, p, q;
    a = q = 1;
    b = p = 0;
    while (n > 0) {
        if (n % 2 == 0) {
            long long qq = q*q;
            q = 2*p*q + qq;
            p = p*p + qq;
            n /= 2;
        } else {
            long long aq = a*q;
            a = b*q + aq + a*p;
            b = b*p + aq;
            n -= 1;
        }
    }
    return b;
}

This is based on the identities of the Lucas sequence.

Tags:

C

Fibonacci