Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute the complexity of this?

int foo(int n)
{
    int sum = 0;
    for(int k=1; k <= n; k = k * 2) 
    {
        sum += k;
    }
    return sum;
}

I have the following function. Now, according to me the runtime complexity of foo(n) should be big-o(logn). Now, I am asked to find out the run time complexity of foo(n*n*n*n). What should it be? According to me, it should be, big-o(logn) only. Am I right in saying this?

like image 570
Qirohchan Avatar asked Feb 13 '23 01:02

Qirohchan


1 Answers

It is O(log n4) → O(4 log n) → O(log n)

like image 88
Paul R Avatar answered Feb 14 '23 16:02

Paul R