typedef unsigned long long ULL; // a simple cache to speedup successive requests! static ULL fib1_cached_index=0; static ULL fib1_cached_value=0; static ULL fib2_cached_index=1; static ULL fib2_cached_value=1; ULL fib(ULL i) { if(i==0)return 0; if(i==1)return 1; if(i==fib1_cached_index)return fib1_cached_value; if(i==fib2_cached_index)return fib2_cached_value; ULL value=fib(i-1)+fib(i-2); fib1_cached_index=fib2_cached_index; fib1_cached_value=fib2_cached_value; fib2_cached_index=i; fib2_cached_value=value; return value; } int main(unsigned int argc, char **argv) { ULL i=0; while(1) { i++; fib(i); //precalc // this will exhaust the stack so we call fib every time above #ifdef __linux__ if((i%100000000)==1)printf("gcc-fibonacci(%llu) = %llu \n",i,fib(i)); #endif if((i%100000000)==1)printf("fibonacci(%u...) = %u... \n",(unsigned)i,(unsigned)fib(i)); } return 0; }