So I was attempting to create a recursive fibonacci series algorithm on my own, similar to this example, just for fun. Then I discovered that its temporal complexity is of the order O(n). However, according to my study, the recursive fibonacci series method has a temporal complexity of O(2n)??
This is my C code:
#include <stdio.h>
void fibonacci(int n){
int static a,b,c; //SO THAT VARIABLES CAN BE SHARED ACROSS FUNCTION CALLS
if (n > 2)
{
fibonacci(n-1);
c = a + b;
printf("%d ",c);
a = b;
b = c;
}
if (n < 3)
{
a = 0;
b = 1;
if (n < 2)
{
printf("%d ",a);
}
else
{
printf("%d %d ",a,b);
}
}
}
int main()
{
fibonacci(10); //FINDING THE FIRST 10 FIBONACCI NUMBERS
return 0;
}
I understand that the use of static variables has altered something, but I’m not sure what and would appreciate a more detailed explanation!
I’ve been having difficulties calculating temporal complexity of recursive algorithms in general, so please forgive me if this is a minor issue. Thanks