Skip to content

a loop inside a loop

foundations

the puzzle

A gardener plants 8 rows of bulbs. Row 1 gets 1 bulb, row 2 gets 2, and so on to row 8. How many bulbs is that, and how many if every row got 8?

a worked example

An outer loop visits 4 hives. In each hive, an inner loop, one inside the outer loop, checks 6 frames, at 1 step a frame.

  1. The inner loop runs in full on every pass of the outer loop.
  2. One hive's inner loop takes 6 steps.
  3. The outer loop runs it 4 times, so 6+6+6+6=4×6=246 + 6 + 6 + 6 = 4 \times 6 = 24 steps.
  4. With 4 hives of 4 frames, it is 4×4=16=424 \times 4 = 16 = 4^2. With nn hives of nn frames, it is n×n=n2n \times n = n^2 steps.
A row of squares for each hive, a square for each frame. 4 rows of 6 make 24 steps.

how it works

In the puzzle, the outer loop runs over the rows, kk from 1 to 8, and on row kk the inner loop plants kk bulbs. The inner loop grows by 1 each pass.

An 8 by 8 square holds 64. The squares on or below the diagonal, 1 to 8 by row, are the bulbs, 36. The 28 above them are the rest of the square.

So the bulbs are 1+2+⋯+81 + 2 + \dots + 8. Lesson 5.2's total gives 8×92=36\frac{8 \times 9}{2} = 36.

If every row got 8 bulbs, the rows would be the same length and make a square, 8×8=648 \times 8 = 64. The growing loop plants a little over half of that.

in symbols

Lesson 7.1 summed loops placed end to end. Here each outer pass repeats the whole inner loop: nn outer passes, each running mm inner steps, take nmnm steps.

A third loop inside multiplies again. With nn passes each, three loops take n×n×n=n3n \times n \times n = n^3, and with 2 passes each, 2×2×2=82 \times 2 \times 2 = 8.

An inner counter halving from nn runs ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor times (lesson 7.1), so nn outer passes take n⌊log⁡2 n⌋n \lfloor \log_2\, n \rfloor steps. For n=16n = 16, the counter runs at 16, 8, 4 and 2, 4 passes, so 16×4=6416 \times 4 = 64.

If pass kk's inner loop runs kk times, the total is ∑k=1nk=n(n+1)2=n2+n2\sum_{k=1}^{n} k = \frac{n(n + 1)}{2} = \frac{n^2 + n}{2}, which is n22+n2\frac{n^2}{2} + \frac{n}{2}.

If pass kk's inner loop runs k−1k - 1 times, the total is 0+1+⋯+(n−1)0 + 1 + \dots + (n - 1). That is nn terms from 0 to n−1n - 1, so lesson 5.2's first form gives n(0+n−1)2=n(n−1)2\frac{n(0 + n - 1)}{2} = \frac{n(n - 1)}{2}. For n=4n = 4, 0+1+2+3=6=4×320 + 1 + 2 + 3 = 6 = \frac{4 \times 3}{2}.

This happens when each new item is checked against every one before it.

So one total is n22+n2\frac{n^2}{2} + \frac{n}{2} and the other n(n−1)2=n2−n2=n22−n2\frac{n(n - 1)}{2} = \frac{n^2 - n}{2} = \frac{n^2}{2} - \frac{n}{2}: half the square, give or take n2\frac{n}{2}.

For n=70n = 70, that is 2450+35=24852450 + 35 = 2485 and 2450−35=24152450 - 35 = 2415: the n2\frac{n}{2} matters less as nn grows.

nnsquare, n2n^2growing, n(n+1)2\frac{n(n + 1)}{2}square less growing, n(n−1)2\frac{n(n - 1)}{2}
2431
8643628
16256136120
70490024852415
The square less the growing staircase is the other staircase, just under half.

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

An outer loop runs over 14 days. Each day, an inner loop checks 9 sensors, at 3 steps a sensor.

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

worked answer
  1. One day's inner loop takes 9×3=279 \times 3 = 27 steps.
  2. The outer loop runs it 14 times, so 14×27=37814 \times 27 = 378 steps.

Check it another way: Counted the other way, 14×9=12614 \times 9 = 126 checks, at 3 steps each, is 126×3=378126 \times 3 = 378.

exercise 2

An outer loop makes 56 passes. On each, an inner counter starts at 56 and is halved, rounding down, while it is above 1. Each inner pass takes 1 step.

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

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

worked answer
  1. The inner counter runs at 56, 28, 14, 7 and 3, and at 1 it stops.
  2. That is 5 passes. As 32≤56<6432 \le 56 < 64, ⌊log⁡2 56⌋=5\lfloor \log_2\, 56 \rfloor = 5 too (lesson 6.4).
  3. The outer loop runs it 56 times, so 56×5=28056 \times 5 = 280 steps.

Check it another way: nn outer passes of a halving inner loop take n⌊log⁡2 n⌋n \lfloor \log_2\, n \rfloor steps, and 56×5=28056 \times 5 = 280.

exercise 3

Three loops sit one inside another, and each runs 6 times. The innermost does 2 steps a pass.

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

worked answer
  1. The innermost loop runs 6×6×6=2166 \times 6 \times 6 = 216 times in all.
  2. At 2 steps a pass, 2×216=4322 \times 216 = 432 steps.

Check it another way: One middle pass takes 6×2=126 \times 2 = 12 steps, one outer pass 6×12=726 \times 12 = 72, and 6 outer passes 6×72=4326 \times 72 = 432.

exercise 4

A loop takes nn photos, one at a time. Each new photo is compared with every photo taken before it, at 1 step a comparison. For nn photos, 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. Photo kk is compared with the k−1k - 1 photos before it, so the steps are 0+1+⋯+(n−1)0 + 1 + \dots + (n - 1).
  2. That is nn terms from 0 to n−1n - 1, so lesson 5.2's first form gives n(n−1)2\frac{n(n - 1)}{2}.
  3. For 15 photos, 15×142=105\frac{15 \times 14}{2} = 105.

Check it another way: For 3 photos, the comparisons are 0, 1 and 2, which is 3, and 3×22=3\frac{3 \times 2}{2} = 3.

exercise 5

An outer loop runs kk from 1 to 26, and on pass kk its inner loop runs kk times, at 1 step each. A square loop, 26 inner steps every pass, would take 262=67626^2 = 676.

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

worked answer
  1. The steps are 1+2+⋯+26=26×2721 + 2 + \dots + 26 = \frac{26 \times 27}{2}.
  2. That is 7022=351\frac{702}{2} = 351.

Check it another way: 2622+262=338+13=351\frac{26^2}{2} + \frac{26}{2} = 338 + 13 = 351, a little over half of 676.