Skip to content

naming how a count grows

foundations

the puzzle

A job does 6 set-up steps. Then a loop of nn passes does 5 steps on each pass, and on pass kk also runs an inner loop of kk passes at 3 steps each. How does it grow?

a worked example

Count first, name last.

  1. Set-up is 6 steps. The loop's own 5 steps a pass make 5n5n.
  2. The inner loop runs 1+2+⋯+n=n(n+1)21 + 2 + \dots + n = \frac{n(n + 1)}{2} times in all, by lesson 7.2, at 3 steps each, so 3n(n+1)2\frac{3n(n + 1)}{2}.
  3. Adding the parts, T(n)=6+5n+3n(n+1)2T(n) = 6 + 5n + \frac{3n(n + 1)}{2}.
  4. Check at n=3n = 3. By hand, 6+15+3×(1+2+3)=396 + 15 + 3 \times (1 + 2 + 3) = 39. The formula gives 6+15+3×3×42=396 + 15 + \frac{3 \times 3 \times 4}{2} = 39.
  5. Multiplied out, 3n(n+1)2=3n22+3n2\frac{3n(n + 1)}{2} = \frac{3n^2}{2} + \frac{3n}{2}.
  6. As 5n=10n25n = \frac{10n}{2}, 5n+3n2=13n25n + \frac{3n}{2} = \frac{13n}{2}, so T(n)=3n22+13n2+6T(n) = \frac{3n^2}{2} + \frac{13n}{2} + 6.
  7. The part that ends up biggest is 3n22\frac{3n^2}{2}. Drop the 32\frac{3}{2} in front, and it grows like n2n^2.
partstepsgrows like
set-up611
the loop's own steps5n5nnn
the inner loop3n(n+1)2\frac{3n(n + 1)}{2}n2n^2
the whole job6+5n+3n(n+1)26 + 5n + \frac{3n(n + 1)}{2}n2n^2
The whole job grows like its part that ends up biggest.

how it works

Each common growth has a name. A count that stays fixed is constant. One like log⁡2 n\log_2\, n is logarithmic, and one like nn linear.

grows likenamea job that does it
11constanta fixed number of steps
log⁡2 n\log_2\, nlogarithmica counter halved down to 1
nnlinearone loop over the items
n log⁡2 nn \,\log_2\, nn log na counter halved down to 1, each pass a loop over all nn
n2n^2quadratica loop inside a loop
n3n^3cubicthree loops each inside the last
2n2^nexponentialsteps that double each time one item is added
The names in the order lesson 7.3 lists them, each ending up bigger than the one above.

One like n2n^2 is quadratic, like n3n^3 cubic, and like 2n2^n exponential. n log⁡2 nn \,\log_2\, n is said "n log n".

The table pairs each name with a job that makes it.

in symbols

The whole method is four moves. Count each part, steps in one pass times the number of passes, closing any sum with day 5's totals.

Join the parts. Add parts that run one after the other, and multiply a loop inside a loop.

Check the full count T(n)T(n) on a small nn. Only then name the part that ends up biggest, without its number in front.

The study pages write "grows like n2n^2" as Θ(n2)\Theta(n^2), said "theta of n squared". So the puzzle's job is Θ(n2)\Theta(n^2).

stepsitems020040060080005101520stepsitems020040060080005101520
  • the inner loop, 3n(n+1)2\frac{3n(n + 1)}{2}
  • the loop's own steps, 5n5n
  • set-up, 6
The inner loop's steps pass the loop's own 5n5n from n=3n = 3 on and pull away, while the set-up stays at 6.

mind these

  • The count is exact. The name is for comparing methods: a quadratic count ends up bigger than a linear one.

your turn

Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.

exercise 1

Give each count's growth its name.

n3+50n2n^3 + 50n^2 is:

8 log⁡2 n+38 \,\log_2\, n + 3 is:

n2+4n log⁡2 nn^2 + 4n \,\log_2\, n is:

worked answer
  1. n3÷50n2=n50n^3 \div 50n^2 = \frac{n}{50} keeps growing, so n3n^3 ends up biggest, and the count is cubic.
  2. 8 log⁡2 n8 \,\log_2\, n ends up past the fixed 3. Drop the 8 in front, and it is logarithmic.
  3. n2n^2 comes after n log⁡2 nn \,\log_2\, n in lesson 7.3's list, so the count is quadratic.

Check it another way: Each name is the growth of the part latest in lesson 7.3's list.

exercise 2

A job does 10 set-up steps and a loop over nn files at 4 steps a file. Then a counter starts at nn and is halved, rounding down, while it is above 1. On each pass a loop reads all nn files, at 1 step a file.

A number, like 13, 9.5 or 19/2.

The job is:

worked answer
  1. The first loop takes 4×64=2564 \times 64 = 256 steps.
  2. The counter runs at 64, 32, 16, 8, 4 and 2, so 6 passes, each reading all 64 files, 6×64=3846 \times 64 = 384.
  3. With set-up, 10+256+384=65010 + 256 + 384 = 650.
  4. In general T(n)=10+4n+n⌊log⁡2 n⌋T(n) = 10 + 4n + n \lfloor \log_2\, n \rfloor, with lesson 6.3's floor.
  5. Rounding log⁡2 n\log_2\, n down takes off less than 1 pass, so less than nn steps in all, and the biggest part still grows like n log⁡2 nn \,\log_2\, n. The job is n log n.

Check it another way: At 64, n log⁡2 n=384n \,\log_2\, n = 384, already past 4n=2564n = 256.

exercise 3

A job does 7 set-up steps. Then on day kk of nn days, a loop restacks 2k2k crates, at 1 step a crate.

Letters and numbers, like 2n + 1.

The job is:

worked answer
  1. The crates are 2+4+⋯+2n2 + 4 + \dots + 2n, which is twice 1+2+⋯+n1 + 2 + \dots + n.
  2. So by lesson 7.2 they are 2×n(n+1)2=n(n+1)2 \times \frac{n(n + 1)}{2} = n(n + 1).
  3. With the 7 set-up steps, T(n)=n(n+1)+7T(n) = n(n + 1) + 7.
  4. n(n+1)=n2+nn(n + 1) = n^2 + n, so the part that ends up biggest is n2n^2, and the job is quadratic.

Check it another way: At n=6n = 6, 2+4+6+8+10+12=422 + 4 + 6 + 8 + 10 + 12 = 42, so 49, and 6×7+7=496 \times 7 + 7 = 49.

exercise 4

A job is a loop inside a loop, both over nn items, so it is quadratic.

The change that makes it linear:

worked answer
  1. Halving the steps puts 12\frac{1}{2} in front, and running twice puts 2 in front. Neither changes the growth.
  2. An inner loop of 5 passes makes n×5=5nn \times 5 = 5n steps, which is linear.

Check it another way: Only a change to how the passes grow with nn changes the name.

exercise 5

Three loops sit each inside the last. The outer two run over nn items, and the innermost runs 10 times, at 1 step a pass.

A number, like 13, 9.5 or 19/2.

The job is:

worked answer
  1. The steps are n×n×10=10n2n \times n \times 10 = 10n^2.
  2. At n=7n = 7, 10×49=49010 \times 49 = 490.
  3. Drop the 10 in front, and it grows like n2n^2, quadratic.

Check it another way: Doubling nn to 14 gives 10×196=196010 \times 196 = 1960, 4 times 490, as a quadratic does.