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 parcels?
1a worked example
Count each part's steps, times how often it runs.
Fetching the tape runs once, at 1 step.
Packing runs once a parcel, so times, at 8 steps each. That is steps (lesson 4.4).
Added, the whole job takes steps.
For 25 parcels, steps.
part
runs
steps each time
steps in all
for 25 parcels
fetch the tape
1
1
1
1
pack a parcel
8
200
added
201
Each part's steps are its runs times its steps each time, and the whole job adds the parts.
2how it works
A loop that does the same steps on each of its passes does steps. Steps done once, before or after it, are added on.
24 halved, rounding down. Passes run at 24, 12, 6 and 3, and at 1 the loop stops.
Not every loop takes 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, , as .
A counter that starts at and halves this way takes passes, so at steps a pass it takes steps. At 3 steps a pass, 24 takes .
3in symbols
Write , said "T of n", for the steps a job of size takes, such as parcels. The brackets name the size and do not mean times. The parcels give , and .
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 passes the counter is , and the loop stops once is 16 or more.
So the passes are the smallest whole at least , which is 5 and a third. That is the ceiling, (lesson 6.3).
Loops that run one after the other add their steps. At , a loop of steps, then one of , makes steps, which is .
A loop that stops early, say once a running total goes over a limit, may take 1 pass or all . The fewest is the best case, the most the worst case. Count the worst case unless told otherwise.
The counter's values, 3 apart and below 16, make six passes.
4your turn
Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.
1exercise 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.
worked answer
At best the first box alone is over 2 metres tall, so 1 pass, at 5 steps.
At worst the stack goes over 2 metres only at the last box, or never, so all 45 passes, at steps.
Check it another way: For boxes, the worst case is , and .
2exercise 2
A counter starts at 500 and is halved, rounding down, while it is above 1. Each pass takes 3 steps.
worked answer
It runs at 500, 250, 125, 62, 31, 15, 7 and 3, and at 1 it stops.
That is 8 passes.
At 3 steps a pass, steps.
Check it another way: , so passes, as lesson 6.4 says.
3exercise 3
A counter starts at 0 and goes up 4 each pass. The loop runs while the counter is below 31.
worked answer
It runs at 0, 4, 8, 12, 16, 20, 24 and 28. At 32 it stops.
That is 8 passes.
Check it another way: After passes the counter is , and it stops once is 31 or more, so .
4exercise 4
A loop makes passes of 4 steps. When it is done, a second loop makes passes of 9 steps.
worked answer
The first loop takes steps, and the second .
The first loop's steps come, then the second's , so their steps add, .
At that is .
Check it another way: Loop by loop, and , and .
5exercise 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 jars, the steps are .
worked answer
Take off the warm-up, which runs once. steps are labelling.