CodeCracker
CodeCracker

Reputation: 9

FIbonacci Time Complexity for non recursive function

Hey guys i need some help for this piece of code, computation had become a problem coz i dont know the exact format in computing this code. any help would do.

int fib(int n)
{
    int prev = -1;
int result = 1;
int sum = 0;


for(int i = 0;i <= n;++ i)
{
    sum = result + prev;
    prev = result;
    result = sum;
}

return result;   

}

Upvotes: 0

Views: 4495

Answers (1)

Dillon
Dillon

Reputation: 76

I am not sure exactly what you are asking, maybe you can clarify

The time complexity of this algorithm is O(n). The loop will execute n times until i is greater than n. i starts at zero and increments by 1 every iteration of this loop until n.

I hope this helps

Upvotes: 3

Related Questions