KonkurStudy
همه کارت‌ها

Order of a Loop's Accumulated Output (Summation Magnitude)

Find the order of the VALUE a loop prints — not its running time — by turning the nested loops into a closed-form sum or product and reading off its leading power.

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

اهمیت
۰/۵

The distinction that traps everyone

Many konkur items ask for the order of the printed value of an accumulator (k, j, s, ...), not the running time. The two are different: a doubly nested loop runs in Θ(n2)\Theta(n^2) time, but the number it accumulates can be Θ(n4)\Theta(n^4), Θ(2n)\Theta(2^n), or anything else, depending on what is added or multiplied each step. Always re-read the question: "order of the output" means analyze the final value of the variable, not the loop count.

The recipe

  1. Express the accumulator as a sum or product over the loop indices. A variable updated by += becomes a ∑\sum; one updated by *= becomes a ∏\prod.
  2. Pin down each index range by tracing the loop guards (mind whether a counter is updated before or after it is used).
  3. Collapse the closed form with the standard identities below.
  4. Keep only the dominant term to get the Θ\Theta-order (for a printed value, report the order, e.g. Θ(n4)\Theta(n^4); for an exact value, simplify fully).

Power-sum identities (the additive toolbox)

∑i=1ni=n(n+1)2=Θ(n2),∑i=1ni2=n(n+1)(2n+1)6=Θ(n3),\sum_{i=1}^{n} i = \frac{n(n+1)}{2} = \Theta(n^{2}),\qquad \sum_{i=1}^{n} i^{2} = \frac{n(n+1)(2n+1)}{6} = \Theta(n^{3}), ∑i=1ni3=(n(n+1)2)2=Θ(n4),in general ∑i=1nip=Θ ⁣(n p+1).\sum_{i=1}^{n} i^{3} = \left(\frac{n(n+1)}{2}\right)^{2} = \Theta(n^{4}),\qquad \text{in general } \sum_{i=1}^{n} i^{p} = \Theta\!\left(n^{\,p+1}\right). For a sum, only the highest power survives in Θ\Theta: 12∑(i3+i2)=Θ(n4)\tfrac12\sum (i^3+i^2)=\Theta(n^4) because ∑i3\sum i^3 dominates ∑i2\sum i^2.

Exponent-sum identity (the multiplicative toolbox)

When the accumulator is a product of powers of a fixed base, the exponents add: ∏t=1kb et=b ∑t=1ket.\prod_{t=1}^{k} b^{\,e_t} = b^{\,\sum_{t=1}^{k} e_t}. The most common pattern is consecutive exponents, where the exponent sum is again triangular: ∏t=1kb t=b 1+2+⋯+k=b k(k+1)/2.\prod_{t=1}^{k} b^{\,t} = b^{\,1+2+\cdots+k} = b^{\,k(k+1)/2}. A product grows exponentially in the loop count — never confuse it with the polynomial growth of a sum.

Mini-example (additive)

s = 0
for(i = 1, i <= n, i++)
    for(j = 1, j <= i, j++)
        s = s + j

Inner loop adds ∑j=1ij=i(i+1)2\sum_{j=1}^{i} j = \tfrac{i(i+1)}{2}. Then s=∑i=1ni(i+1)2=12∑i=1n(i2+i)=Θ(n3).s = \sum_{i=1}^{n}\frac{i(i+1)}{2} = \frac12\sum_{i=1}^{n}(i^{2}+i) = \Theta(n^{3}). The running time is only Θ(n2)\Theta(n^2), but the printed value is order n3n^{3}.

Mini-example (multiplicative)

If pp is multiplied by 2t2^t on pass tt for t=1,…,kt=1,\dots,k, then p=∏t=1k2t=2k(k+1)/2p = \prod_{t=1}^{k} 2^{t} = 2^{k(k+1)/2} — exponential in kk.

Pitfall: "Order of the output" ≠\neq "time complexity." A counter incremented inside the inner loop measures steps (Θ(n2)\Theta(n^2) here), but the value built by adding i⋅ji\cdot j can be a full degree higher (Θ(n4)\Theta(n^4)). Decide first whether you are counting iterations or summing what each iteration contributes.

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

Sum of p-th powers (order)
∑i=1nip=Θ ⁣(n p+1)\sum_{i=1}^{n} i^{p} = \Theta\!\left(n^{\,p+1}\right)

A sum of i^p over i=1..n has order n^(p+1); only the highest power of i present survives in Theta.

Sum of cubes (closed form)
∑i=1ni3=(n(n+1)2)2=Θ(n4)\sum_{i=1}^{n} i^{3} = \left(\frac{n(n+1)}{2}\right)^{2} = \Theta(n^{4})

Triangular-number squared; the dominant term when a nested loop accumulates i*j.

Triangular number
∑j=1ij=i(i+1)2\sum_{j=1}^{i} j = \frac{i(i+1)}{2}

Closed form for the inner sum 1+2+...+i; substitute it before summing over the outer index.

Product of powers (exponents add)
∏t=1kb t=b 1+2+⋯+k=b k(k+1)/2\prod_{t=1}^{k} b^{\,t} = b^{\,1+2+\cdots+k} = b^{\,k(k+1)/2}

A loop that multiplies by b^t each pass yields b raised to the triangular sum of exponents — exponential in the loop count k.

#complexity#summation#asymptotics#power-sums#loop-analysis#output-value#theta-order#accumulator

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