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

Elegant as fuck

Name: Anonymous 2011-05-15 20:42

def fib(times):
    a, b = 0,1
    for _ in range(times):
        a, b = b, a+b
    return a


I think I just creamed my pants.

Name: Anonymous 2011-05-17 2:24


#include <stdio.h>

#define NMAX 10001

void fib (unsigned int times)
{
    unsigned char a[NMAX] = {0}, b[NMAX] = {0}, temp[NMAX] = {0};
    unsigned int i, t = 0;
    b[0] = 1; b[1] = 0; a[0] = a[1] = 1;
    while (times-- - 1)
    {
        for (i = 0; i < a[0]+1; i++)
            temp[i] = a[i];
        for (i = 1; i <= a[0] || i <= b[0] || t; i++, t /= 10)
            a[i] = (t += a[i] + b[i]) % 10;
        a[0] = i-1;
        for (i = 0; i < a[0]+1; i++)
            b[i] = temp[i];
    }
    for (i = a[0]; i > 0; i--)
        printf ("%d", a[i]);
}

int main (void)
{
    fib (1000);
    return 0;
}


There, now it works with bignums, not very elegant though. (I'm the C guy from >>13)

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