#include #include #include using namespace std; const int HIGH_LIMIT = 44; const int LOW_LIMIT = 0; unsigned long Fib(int n); unsigned long MemoFib(int n); unsigned long ItFib(int n); vector CALLS(HIGH_LIMIT+1,0); int main() { int i{20}; cout << "Recursive regular fib " << endl; for(i = LOW_LIMIT; i <= HIGH_LIMIT; i++) { cout << setw(3) << i << setw(20) << Fib(i) << endl; } cout << endl << endl; cout << "Call Record " << endl; for(i =0; i <= HIGH_LIMIT; i ++) { cout << i << " " << CALLS[i] << endl; } cout << endl << endl; cout << "MemoFib " << endl; for(i = LOW_LIMIT; i <= HIGH_LIMIT; i++) { cout << setw(3) << i << setw(20) << MemoFib(i) << endl; } cout << endl << endl; cout << "Iterative Fib" << endl; for(i = LOW_LIMIT; i <= HIGH_LIMIT; i++) { cout << setw(3) << i << setw(20) << ItFib(i) << endl; } return 0; } unsigned long Fib(int n){ CALLS[n] ++; if (n <= 1) { return 1l; } return Fib(n-1) + Fib(n-2); } unsigned long MemoFib(int n){ static vector history{1,1}; unsigned long tmp; if (static_cast(n) >= history.size()) { tmp = MemoFib(n-1) + MemoFib(n-2); history.push_back(tmp); } return history[n]; } unsigned long ItFib(int n) { unsigned long prev {1}; unsigned long last {1}; int i; unsigned long sum{1}; for(i = 2; i <= n; i++) { sum = prev + last; last = prev; prev = sum; } return sum; }