/* Fast Fibonacci. */ long long int fastfib(int n) { fastfib_execs++; if (n <= 2) return (n+1)/2; int half = n/2; return n % 2 == 0 ? fastfib(half) * (2 * fastfib(half + 1) - fastfib(half)) : sq(fastfib(half+1)) + sq(fastfib(half)); }
int main() { unsigned int n = 0; if (n == 0) { if (n == 1) { printf("Hello\n!"); } } for(n=0; n<35; ++n) printf("fib(%u)=%u\n", n, fib(n)); for(n=0; n<35; ++n) printf("fastfib(%u)=%u\n", n, fastfib(n)); for(n=0; n<35; ++n) printf("fastfib_v2(%u)=%u\n", n, fastfib(n)); return 0; }
/* Dynamic programming. */ int main(int argc, char **argv) { int n; n = 30; /* Table. */ int size = 100; if (!(table = calloc(size, sizeof(long long int)))) return 1; /* Execution counts. */ dynpfib_execs = 0; fastfib_execs = 0; fib_execs = 0; dynpfib(n); fastfib(n); fib(n); /* Times. */ printf("usual=%lld fast=%lld dynprog=%lld\n", fib_execs, fastfib_execs, dynpfib_execs); return EXIT_SUCCESS; }