Run
#include <bits/stdc++.h>
using namespace std;
map<int,int> m;
int Fibonacci(int n)
{
cout<<"Fibonacci ("<<n<<") is called "<<m[n]++<<" times."<<endl;
if(n==0|n==1) return n;
int a=Fibonacci(n-1)+Fibonacci(n-2);
return a;
}
int main()
{
cout<<"This is Now the 5th Fibonacci Number in Recursive way: \n";
cout<<Fibonacci(5);
}
Output:
This is Now the 5th Fibonacci Number in Recursive way:
Fibonacci (5) is called 0 times.
Fibonacci (4) is called 0 times.
Fibonacci (3) is called 0 times.
Fibonacci (2) is called 0 times.
Fibonacci (1) is called 0 times.
Fibonacci (0) is called 0 times.
Fibonacci (1) is called 1 times.
Fibonacci (2) is called 1 times.
Fibonacci (1) is called 2 times.
Fibonacci (0) is called 1 times.
Fibonacci (3) is called 1 times.
Fibonacci (2) is called 2 times.
Fibonacci (1) is called 3 times.
Fibonacci (0) is called 2 times.
Fibonacci (1) is called 4 times.
5
Login/Signup to comment