#include <iostream>
#include <vector>
#include <unordered_map>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int fib_topDown(int n, std::unordered_map<int, int>& memo)
{
if (n <= 1)
return 1;
auto result = memo.find(n);
if (result != memo.end())
return result->second;
return memo[n] = fib_topDown(n - 1, memo) + fib_topDown(n - 2, memo);
}
int fib_buttonUp(int n)
{
std::vector<int> results(n + 1);
results[0] = 0;
results[1] = 1;
for (int i = 2; i <= n; i++)
results[i] = results[i - 1] + results[i - 2];
return results.back();
}
int main(int argc, char *argv[]) {
std::cout << fib(40) << std::endl;
std::unordered_map<int, int> memo;
memo[1] = 0;
memo[2] = 1;
std::cout << fib_topDown(40, memo) << std::endl;
std::cout << fib_buttonUp(40) << std::endl;
getchar();
return 0;
}