Logarithmic
The second for-loop never stops ( 0 * 2 = 0 ). I think you asked about this loop:
for (int i = 1; i < n; i = 2 * i) a = i;
In this case, the complexity expressed through the designation of the sum will be:
Sum 1, i = 1 - log (n-1) == O (log n)
Triple nested
In this case, it will be a summation:
number of steps sum -------------------------------------- 1 1 1 1 1 1 . n 2 2 2 2 2 . 2(n-1) 3 3 3 3 . 3(n-2) 4 4 4 . 4(n-3) . . . . n-1 n-1 2(n-1) nn
or alternatively if I transpose the triangle:
number of steps sum -------------------------------------- 1 2 3 4 . n-1 nn(n+1)/2 1 2 3 4 . n-1 (n-1)(n)/2 1 2 3 4 . (n-2)(n-1)/2 1 2 3 . 4(n-3) 1 2 . . 1 . 3 . 1
The numbers on the right (in the second triangle) are also called the numbers of the triangles . So the question is equivalent
"What is the sum of the numbers of the triangle less than or equal to f (n). (F (1) + f (2) + f (n), where f (x) = x (x + 1) / 2)."
The answer to this question is:
f (n) = n (n + 1) (n + 2) / 6
The proof is here .
Thus, the resulting complexity in large-o O (n ^ 3)
source share