/* Which is your favorite Fibonacci function? */ #include int fib1(int n) { if (n<2) return 1; else return fib1(n-1) + fib1(n-2); } int fib2(int n, int a, int b) { if (n<2) return b; else return fib2(n-1,b,a+b); } // this function is meant to be called with 1,1 for a and b. int fib3(int n) { int a = 1, b = 1; int temp; while (n>1) { temp = b; b = a+b; a = temp; n--; } return b; } int main(int arc, char **argv) { int n = atoi(argv[1]); // use command line arg for value printf("the %dth Fibonacci number is %d\n", n,fib1(n)); printf("with tail recursion it's also %d\n", fib2(n,1,1)); printf("without recursion it's still %d\n", fib3(n)); exit(0); }