Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fibs in C

Name: Anonymous 2008-06-14 10:50

How can you write the Haskell fibs in C? I mean the

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibs.

Name: Anonymous 2008-06-14 11:01

int fibs(int no){
    while(no>fibslist_last){
        fibslist_last++;
       
        fibslist[fibslist_last]=
            fibslist[fibslist_last-1]+fibslist[fibslist_last-2];
    }
    return fibslist[no];
}

Name: Anonymous 2008-06-15 10:39

Fuck.

int fibarray[MAX_FIBS] = {1,1};

int fib(int n)
{
   return fibarray[n] ? fibarray[n]
                      : fibarray[n] = fib(n-1) + fib(n-2);
}

Name: Anonymous 2008-06-15 12:05

OMG OPTIMIZED

int fibarray[MAX_FIBS] = {1,1};
int last = 1;

int fib(int n)
{
   while(last < n) fibarray[last+1] = fibarray[last++] + fibarray[last-1];
   return fibarray[n];
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List