KonkurStudy
همه کارت‌ها

Loop Iteration Count from the Update Rule (i+1 vs i2 vs ii)

The cost of a single counted loop is set by how the counter is UPDATED, not how it is compared. Solve the recurrence on the counter, set it equal to the bound n, and invert: i+=c gives n/c steps, i*=c gives log_c n steps, and i=i*i gives log log n steps.

کارت مفهوم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

The core idea

For a loop of the shape

i = i0;
while (i <= n) {
  /* O(1) body */
  i = f(i);
}

the running time is Θ(number of iterations)\Theta(\text{number of iterations}) when the body is O(1)O(1). The iteration count is decided entirely by the update rule i←f(i)i \leftarrow f(i). The recipe is always the same three steps:

  1. Track the counter. Write iki_k, the value of ii after kk executions of the body, as a closed form in kk.
  2. Find the stopping point. The body runs while ik≤ni_k \le n; solve ik≤ni_k \le n for kk.
  3. Invert to get kk. Whatever function grew ii is inverted to count the steps: repeated addition inverts to division, repeated multiplication inverts to a logarithm, repeated squaring inverts to a double logarithm.

The three update families

Update rule f(i)f(i) Closed form iki_k (from i0i_0) Stops when Iterations Θ(⋅)\Theta(\cdot)
i←i+ci \leftarrow i + c (additive) i0+c ki_0 + c\,k i0+ck>ni_0 + ck > n Θ(n)\Theta(n)
i←i⋅ci \leftarrow i \cdot c (multiplicative, c>1c>1) i0⋅c ki_0 \cdot c^{\,k} i0ck>ni_0 c^{k} > n Θ(log⁡cn)=Θ(log⁡n)\Theta(\log_c n)=\Theta(\log n)
i←i⋅ii \leftarrow i \cdot i (squaring) i0 2ki_0^{\,2^{k}} i02k>ni_0^{2^{k}} > n Θ(log⁡log⁡n)\Theta(\log\log n)

The pattern is a clean ladder: each time you make the update one operation "stronger" (add →\to multiply →\to square), you peel off one logarithm from the iteration count. Going the other way (e.g. i←ii \leftarrow \sqrt{i} shrinking down to a constant) also yields Θ(log⁡log⁡n)\Theta(\log\log n) by the same inversion.

Why squaring gives log⁡log⁡n\log\log n

Start at i0=2i_0 = 2 and square each step. The exponent doubles every iteration:

i0=2,i1=22,i2=24,i3=28, …,ik=22k,i_0 = 2,\quad i_1 = 2^{2},\quad i_2 = 2^{4},\quad i_3 = 2^{8},\ \dots,\quad i_k = 2^{2^{k}},

because (22k)2=22k⋅2=22k+1\left(2^{2^{k}}\right)^{2} = 2^{2^{k}\cdot 2} = 2^{2^{k+1}}. The loop stops when

22k>n⟺2k>log⁡2n⟺k>log⁡2 ⁣(log⁡2n).2^{2^{k}} > n \quad\Longleftrightarrow\quad 2^{k} > \log_2 n \quad\Longleftrightarrow\quad k > \log_2\!\left(\log_2 n\right).

Taking log⁡2\log_2 once undoes the outer power; taking it a second time undoes the doubling of the exponent. So the number of iterations is Θ(log⁡log⁡n)\Theta(\log\log n) — and since the body is O(1)O(1), the total time is Θ(log⁡log⁡n)\Theta(\log\log n).

How to apply it

  • Read the update line, not the test. The comparison i <= n only tells you the bound; the growth speed comes from i = f(i).
  • The starting value i0i_0 only changes lower-order constants. As long as i0≥2i_0 \ge 2 for a multiplicative/squaring loop, the asymptotic order is unchanged. (Beware i0=1i_0 = 1 with i = i*i — it never grows: 1⋅1=11\cdot1=1 — an infinite loop, a classic trap.)
  • For nested loops, multiply the per-loop counts. A *2 outer loop wrapping a +1 inner loop is Θ(nlog⁡n)\Theta(n\log n), etc.

Worked mini-example

How many times does the body run?

i = 2;
while (i <= n) { i = i * i; }

The update is squaring, so ik=22ki_k = 2^{2^{k}}. It stops when 22k>n2^{2^{k}} > n, i.e. k>log⁡2log⁡2nk > \log_2 \log_2 n. Hence the body runs Θ(log⁡log⁡n)\Theta(\log\log n) times and the loop is Θ(log⁡log⁡n)\Theta(\log\log n). Contrast the same loop bound with a multiplicative update i = i * 2, which would give ik=2k+1i_k = 2^{k+1}, stopping at k≈log⁡2nk \approx \log_2 n — an exponentially larger Θ(log⁡n)\Theta(\log n) — showing how much the single update operator matters.

Exam trick: Don't be fooled by i <= n into guessing Θ(n)\Theta(n). Identify the update operator: + ⇒Θ(n)\Rightarrow \Theta(n), \times ⇒Θ(log⁡n)\Rightarrow \Theta(\log n), squaring ⇒Θ(log⁡log⁡n)\Rightarrow \Theta(\log\log n). Each step up in operator strength removes one log⁡\log.

فرمول‌های کلیدی

Counter after k iterations (general)
ik=f(k)(i0)i_k = f^{(k)}(i_0)

i_k is the update rule f applied k times to the start value; solve i_k <= n for k to get the iteration count.

Additive loop (i += c)
ik=i0+c k≤n ⟹ k=Θ(n)i_k = i_0 + c\,k \le n \ \Longrightarrow\ k = \Theta(n)

Repeated addition inverts to division: the counter reaches n in linearly many steps.

Multiplicative loop (i *= c, c>1)
ik=i0 c k≤n ⟹ k=Θ(log⁡cn)=Θ(log⁡n)i_k = i_0\,c^{\,k} \le n \ \Longrightarrow\ k = \Theta(\log_c n) = \Theta(\log n)

Repeated multiplication inverts to a logarithm; the base only changes a constant factor.

Squaring loop (i = i*i)
ik=i0 2k≤n ⟹ k=Θ(log⁡log⁡n)i_k = i_0^{\,2^{k}} \le n \ \Longrightarrow\ k = \Theta(\log\log n)

Squaring doubles the exponent each step, so inverting it needs two logarithms.

#loops#iteration-count#time-complexity#asymptotics#logarithmic#log-log-n#update-rule#growth-rate

سوالات تمرینی