This is similar to what matsp posted, except it will take arbitrary numbers:
Code:
typedef unsigned long long ull;
ull fib_t (int num, ull * precalc) {
if (num <= 2) {
return 1;
}
if (precalc[num] == 0) {
precalc[num] = fib_t(num-1, precalc) + fib_t (num-2, precalc);
printf ("%d = %llu\n", num, precalc[num]);
}
return precalc[num];
}
int main (int argc, char ** argv) {
int num = 0;
ull * table = 0;
if (argc != 2) {
printf ("You must specifiy a number\n");
return 1;
}
num = atoi (argv[1]);
table = calloc (num+1, sizeof (ull));
if (table == 0) {
printf ("malloc failed!\n");
return 1;
}
printf ("Fibonacci Number for %d = %llu\n", num, fib_t(num, table));
free (table);
return 0;
}
It even prints out all the numbers as it computes them
QuantumPete