I have a program that prints Fibonacci Series using Recursion. There are better methods for it, but I was asked to use recursion so i had to do it this way.
Here is the program:
#include <stdio.h>
#define TERMS 10
long fibo(int);
int main(void){
for(int i = 1; i <= TERMS; i++) {
printf("%ld", fibo(i));
}
return 0;
}
long fibo(int n){
if (n < 3) {
return 1;
}
else {
return fibo(n - 1) + fibo(n - 2);
}
}
I know this is really a bad approach for the Fibonacci Series and this is clear from above as TERMS get to be more than 35, the program takes a lot of time to complete.
I have gone through this answer and could not understand how they solved it but it looks like
Time complexity for fibo(int n) is O(2^n)
Again I maybe completely wrong, but all I want to is :
What is the Time-Complexity for this complete program, Explain briefly how you calculated it?
If you have a better approach for calculating Fibonacci using recursion, it is also welcome.