Skip to content

Fast and Elegant Fibonnaci algorithm

/*
After playing with some ideas, i've come up with this algorithm for this
common programming exercise.
Have fun with it.
Its the fastest I have found, the complexity is liniar and the for N = 23000
its processing in about 10 ms;
The result is about 3.22e-319 */

#include <iostream>
int main (int argc, const char * argv[])
{
    int n = 23000;
    if (n <= 2)
        printf("%d", 1);
    else {
        long double box[3];
        int i;
        for(i = 0; i < n; i++) {
            if(i <= 1) {
                box[0] = 1;
                box[1] = 1; }
            if(i > 1) {
                box[0] = box[1];
                box[1] = box[2]; }
            box[2] = box[0] + box[1];
        }
        printf("%.Lf", box[2]);
    }
return 0;}
Follow

Get every new post delivered to your Inbox.