Skip to content

T.11 · Weak compositions, loop invariants and flowchart tracing

GRE MathematicsTopic 26

Video lesson for this topic Open the video page
4:51

T.11 · Weak compositions, loop invariants and flowchart tracing

In 1969 Tony Hoare, a British computer scientist, published "An Axiomatic Basis for Computer Programming", building on work by Robert Floyd. Its central idea…

English narration · English and 中文 subtitles you can switch

26.1

Weak compositions, loop invariants and flowchart tracing

In 1969 Tony Hoare, a British computer scientist, published "An Axiomatic Basis for Computer Programming", building on work by Robert Floyd. Its central idea was simple: to prove a loop correct, find a statement that is true before the loop starts and stays true after every pass. When the loop stops, that statement, together with the stopping condition, gives the answer. This loop invariant 循环不变量 turns tracing a program into a short proof. Hoare later received the Turing Award.

GRE Mathematics counting and algorithm questions ask you to:

  • Count each weak composition 弱组合分拆 with stars and bars.
  • Handle lower bounds by shifting and upper bounds by inclusion–exclusion.
  • Trace loops and flowcharts, counting iterations.
  • State and use a loop invariant.
Vocabulary Train
English
loop invariant/luːp ɪnˈveərɪənt/
weak composition/wiːk ˌkɒmpəˈzɪʃn/
26.2

Stars and bars

The number of solutions of $x_1 + \cdots + x_k = n$ in nonnegative integers is

$$\binom{n + k - 1}{k - 1}$$
  • Picture $n$ stars and $k - 1$ bars in a row; the bars split the stars into $k$ groups.
  • Positive integers (each $x_i \ge 1$): $\dbinom{n - 1}{k - 1}$.
  • Lower bounds: substitute $x_i = u_i + m_i$ to remove each bound $x_i \ge m_i$.
  • Upper bounds: count all solutions, then subtract those that break a bound, with inclusion–exclusion.
Two stars, a bar, three stars, a bar: 5 identical items in 3 boxes as 2, 3, 0; choosing 2 bar places in 7 slots gives 21 solutions
Stars and bars: C(n + k - 1, k - 1).
26.3

Tracing algorithms

  • Make a table with one column per variable and one row per pass, and update the variables in the order the code does.
  • Check the loop condition at the right moment: before the body for "while", after it for "repeat until".
  • Count iterations carefully: off-by-one errors are the commonest mistake.
26.4

Loop invariants

A loop invariant is a statement that is:

  • True before the first iteration.
  • Kept true by every iteration.
  • Useful at the end: combined with the stopping condition, it gives the result.
Table for an odd-sum loop: after m iterations the sum S is m² and the next addend B is 2m + 1
The loop invariant S = m² holds before each addition
26.5

Worked example: bounded solutions

Count nonnegative solutions of $a + b + c = 8$ with $a \ge 2$.

  • Put $u = a - 2$: $u + b + c = 6$ has $\binom82 = 28$ solutions.
  • With also $b, c \ge 1$: subtract 1 from each to get a total of 4, so $\binom62 = 15$.
  • So $28 - 15 = 13$ have $b = 0$ or $c = 0$. Check: $b = 0$ gives 7, $c = 0$ gives 7, both zero is counted twice: $7 + 7 - 1 = 13$.
26.6

Worked example: an odd-number loop

S = 0; B = 1
while B <= 2n - 1:
    S = S + B
    B = B + 2
output S
  • Invariant after $m$ passes: $S = m^2$ and $B = 2m + 1$. It holds at $m = 0$, and one pass gives $S = m^2 + 2m + 1 = (m + 1)^2$, $B = 2(m + 1) + 1$.
  • The loop stops when $B = 2n + 1$, that is after $n$ passes, so the output is $n^2$. For $n = 4$: 16.
  • The loop while x > 1: x = x // 2, from $x = 100$, runs through 50, 25, 12, 6, 3, 1: six iterations.
26.7

Common errors

  • Using $\binom{n + k - 1}{k}$ with the wrong lower index, or mixing up nonnegative and positive solutions.
  • Forgetting to shift lower bounds before counting.
  • Counting a loop condition check as an extra pass.
  • Stating an invariant that is true at the end but not kept by each pass.
  • Updating two variables in the wrong order when tracing.
26.8

Key terms

weak composition: An ordered way of writing a number as a sum of nonnegative integers.

loop invariant: A statement that is true before and after every iteration of a loop.

26.9

Check yourself

  • How many nonnegative solutions does $x + y + z = 10$ have?
  • How many positive solutions does $a + b + c + d = 10$ have?
  • What does the odd-number loop output for $n = 7$?

Answers: $\binom{12}{2} = 66$. $\binom93 = 84$. 49.

Was this helpful?

Interactive lessons on this topic

Work through it step by step, with instant-check exercises.

More topics in GRE Mathematics

Log in or create account

IGCSE, A-Level & AP