Skip to content

one loop's step count

foundations

the puzzle

Packing a parcel takes 8 steps, and fetching the tape first takes 1. How many steps does packing 25 parcels take, and how many for nn parcels?

a worked example

Count each part's steps, times how often it runs.

  1. Fetching the tape runs once, at 1 step.
  2. Packing runs once a parcel, so nn times, at 8 steps each. That is ∑k=1n8=8n\sum_{k=1}^{n} 8 = 8n steps (lesson 4.4).
  3. Added, the whole job takes 8n+18n + 1 steps.
  4. For 25 parcels, 8×25+1=2018 \times 25 + 1 = 201 steps.
partrunssteps each timesteps in allfor 25 parcels
fetch the tape1111
pack a parcelnn88n8n200
added8n+18n + 1201
Each part's steps are its runs times its steps each time, and the whole job adds the parts.

how it works

A loop that does the same cc steps on each of its nn passes does cncn steps. Steps done once, before or after it, are added on.

rounding down2412631.5 → 14 halvings
24 halved, rounding down. Passes run at 24, 12, 6 and 3, and at 1 the loop stops.

Not every loop takes nn passes. Start a counter at 24 and halve it, rounding down, while it is above 1. It runs at 24, 12, 6 and 3, and at 1 it stops, so 4 passes.

That is lesson 6.4's count, ⌊log⁡2 24⌋=4\lfloor \log_2\, 24 \rfloor = 4, as 24=16≤24<32=252^4 = 16 \le 24 < 32 = 2^5.

A counter that starts at nn and halves this way takes ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor passes, so at cc steps a pass it takes c⌊log⁡2 n⌋c \lfloor \log_2\, n \rfloor steps. At 3 steps a pass, 24 takes 3×4=123 \times 4 = 12.

in symbols

Write T(n)T(n), said "T of n", for the steps a job of size nn takes, such as nn parcels. The brackets name the size and do not mean times. The parcels give T(n)=8n+1T(n) = 8n + 1, and T(25)=201T(25) = 201.

A counter can skip ahead. Say it starts at 0, goes up 3 each pass, and the loop runs while the counter is below 16. It runs at 0, 3, 6, 9, 12 and 15, so 6 passes, and at 18 it stops.

After kk passes the counter is 3k3k, and the loop stops once 3k3k is 16 or more.

So the passes are the smallest whole kk at least 163\frac{16}{3}, which is 5 and a third. That is the ceiling, ⌈163⌉=6\lceil \frac{16}{3} \rceil = 6 (lesson 6.3).

Loops that run one after the other add their steps. At n=4n = 4, a loop of 2n2n steps, then one of 3n3n, makes 8+12=208 + 12 = 20 steps, which is 5n5n.

A loop that stops early, say once a running total goes over a limit, may take 1 pass or all nn. The fewest is the best case, the most the worst case. Count the worst case unless told otherwise.

012345678910111213141516+3+3+3+3+3
The counter's values, 3 apart and below 16, make six passes.

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

A loop stacks up to 45 boxes of different heights, one at a time, and stops once the stack is over 2 metres tall. Each box takes 5 steps.

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

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

worked answer
  1. At best the first box alone is over 2 metres tall, so 1 pass, at 5 steps.
  2. At worst the stack goes over 2 metres only at the last box, or never, so all 45 passes, at 5×45=2255 \times 45 = 225 steps.

Check it another way: For nn boxes, the worst case is T(n)=5nT(n) = 5n, and T(45)=225T(45) = 225.

exercise 2

A counter starts at 500 and is halved, rounding down, while it is above 1. Each pass takes 3 steps.

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

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

worked answer
  1. It runs at 500, 250, 125, 62, 31, 15, 7 and 3, and at 1 it stops.
  2. That is 8 passes.
  3. At 3 steps a pass, 3×8=243 \times 8 = 24 steps.

Check it another way: 28=256≤500<512=292^8 = 256 \le 500 < 512 = 2^9, so ⌊log⁡2 500⌋=8\lfloor \log_2\, 500 \rfloor = 8 passes, as lesson 6.4 says.

exercise 3

A counter starts at 0 and goes up 4 each pass. The loop runs while the counter is below 31.

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

worked answer
  1. It runs at 0, 4, 8, 12, 16, 20, 24 and 28. At 32 it stops.
  2. That is 8 passes.

Check it another way: After kk passes the counter is 4k4k, and it stops once 4k4k is 31 or more, so ⌈314⌉=8\lceil \frac{31}{4} \rceil = 8.

exercise 4

A loop makes nn passes of 4 steps. When it is done, a second loop makes nn passes of 9 steps.

The two loops take:

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

worked answer
  1. The first loop takes 4n4n steps, and the second 9n9n.
  2. The first loop's 4n4n steps come, then the second's 9n9n, so their steps add, 4n+9n=13n4n + 9n = 13n.
  3. At n=20n = 20 that is 13×20=26013 \times 20 = 260.

Check it another way: Loop by loop, 4×20=804 \times 20 = 80 and 9×20=1809 \times 20 = 180, and 80+180=26080 + 180 = 260.

exercise 5

A machine warms up for 5 steps, then labels jars, the same number of steps for each jar. 30 jars take 215 steps in all. For nn jars, the steps are T(n)T(n).

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

Letters and numbers, like 2n + 1.

worked answer
  1. Take off the warm-up, which runs once. 215−5=210215 - 5 = 210 steps are labelling.
  2. 30 jars share them, so each takes 210÷30=7210 \div 30 = 7 steps.
  3. For nn jars, labelling takes 7n7n steps, so T(n)=7n+5T(n) = 7n + 5.

Check it another way: T(30)=7×30+5=215T(30) = 7 \times 30 + 5 = 215, the steps given.