0
i=n; 
while(i>0) {
  k=1;
  for(j=1;j<=n:j+=k)
    k++;
  i=i/2;
}

The while loop has the complexity of $\lg(n)$ the j value of inner loop runs 1,3,6,10,15... increase like 2,3,4,5,...

But how to find the overall complexity ?

Raphael
  • 72,336
  • 29
  • 179
  • 389
Xax
  • 35
  • 8

1 Answers1

1

$j$ satisfies the recurrence $j_k = j_{k-1}+k$. Note that another sequence, $s_k = k^2$ satisfies $s_k = s_{k-1} + 2k-1$.

$$j_k = \sum_{i=1}^k i = \frac 1 2\sum_{i=1}^k 2i = \frac 1 2(s_k+k)$$

This should give you enough to find the largest $k$ such that $j_k < n$.

Karolis Juodelė
  • 3,667
  • 15
  • 18