Nested Loop Iteration Count -
sorry if i'm re-asking previous question can't quite find concrete answer question. how can make formula nested loop iterations besides basic ones like:
for (int =0; < n; i++)
i basic concept of count iterations of basic loops:
for (int =0; < n; i++)
the boolean condition equal variable (for instance n) subtracted initial variable (for instance i) divided number of loops nested (in case 1 since not nested). number of iterations loop be:
(n - i) / 1
for example, finding iterations of nested loops repeated down loops until make innermost loop multiple loops iteration count.
i don't understand more complicated loops different increment conditions such multiplication or division. how can figure out how many times loop iterates:
for (int = 1; < 1000; *= 2) (int j = 0; j < 1000; j++)
i know has summation unfortunately i'm not seeing connection. resources or advice appreciated.
just work out how many times each 1 loops. easy because not co-dependent (ie j
loop not rely on i
).
the
i
loop goes1, 2, 4, 8, 16, ..., 512
. since must less 1000, stop when reaches 1024. that's total of 10 iterations. count them hand, or calculatelog2(1024)
.the
j
loop goes0, 1, 2, 3, ..., 999
. that's total of 1000 iterations.
so have inner loop of 1000 iterations repeated 10 times outer loop. that's total of 10,000 iterations.
Comments
Post a Comment