Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int fib(int n)
- {
- if (n == 0) return 0;
- if (n == 1) return 1;
- return fib(n - 1) + fib(n - 2);
- }
- int fib1(int n)
- {
- int res{ 0 };
- int arr[2]{ 0 , 1 };
- if (n == 0) return 0;
- if (n == 1) return 1;
- for (int i{ 2 }; i <= n; ++i)
- {
- res = arr[0] + arr[1];
- arr[0] = arr[1];
- arr[1] = res;
- }
- return res;
- }
- int main()
- {
- const int maxNum{ 45 };
- for (int i{ 0 }; i < maxNum; ++i)
- {
- std::cout << fib(i) << ' ';
- }
- std::cout << '\n';
- for (int i{ 0 }; i < maxNum; ++i)
- {
- std::cout << fib1(i) << ' ';
- }
- std::cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement