Advertisement
Josif_tepe

Untitled

Nov 20th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. int fib(int n) {
  8.     if(n == 1 or n == 2) {
  9.         return 1;
  10.     }
  11.     return fib(n - 1) + fib(n - 2);
  12. }
  13. int main() {
  14.    
  15.     cout << fib(4) << endl;
  16.    
  17.     return 0;
  18. }
  19. // fib(4) = fib(3) + fib(2) = 2 + 1 = 3
  20. // fib(3) = fib(2) + fib(1) = 1 + 1 = 2
  21. // fib(2) = 1
  22. // fib(1) = 1
  23. // fib(2) = 1
  24.  
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement