Advertisement
smatskevich

Fibonacci

Dec 19th, 2020
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.30 KB | None | 0 0
  1. ll Fib(int n) {
  2.   if (n == 0 || n == 1) return 1ll;
  3.   ll prev = 1ll;
  4.   ll current = 1ll;
  5.   for (int i = 0; i < n - 1; ++i) {
  6.     ll next = prev + current;
  7.     prev = current;
  8.     current = next;
  9.   }
  10.   return current;
  11. }
  12.  
  13. int main() {
  14.   int n = 0;
  15.   std::cin >> n;
  16.   std::cout << Fib(n);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement