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 ?
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 ?
$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$.
for(k = 1; s_k < n; k++)
the complexity would be $O(\sqrt n)$.
– Karolis Juodelė
Jan 03 '14 at 14:35
k++
in the inner loop makes things more interesting. Still, please read the questions I linked and present a more elaborate attempt which we can then work off. – Raphael Jan 03 '14 at 16:00