Induction Over Iteration Counts
Induction can prove a state property for zero completed iterations and preserve it through an arbitrary next iteration.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Prove a triangular total
Suppose a loop adds \(i\) at step \(i\) starting at one. At zero the sum is zero. If it equals \(k(k+1)/2\) after k steps, adding \(k+1\) gives \((k+1)(k+2)/2\). Loop invariants connect this count to actual state.
Know which statements you proved
The induction establishes the sum after any completed k. To know a program reaches the requested k, check its bounds and progress conditions separately.[1]
Make the prefix invariant do the work
A loop appends twice each of the integers from zero through \(n-1\). After \(k\) completed iterations, the result has length \(k\) and position \(j<k\) contains \(2j\). Initialization uses the empty list. One append leaves older positions unchanged and writes the promised next position. See worked loop invariants.
k=0: []
k=1: [0]
k=2: [0,2]
k=3: [0,2,4]
State when the induction stops
The invariant is true at intermediate counts as well as completion. The loop's bound shows it finishes exactly at \(k=n\). A failing trace can reveal whether the defect lies in initialization, preservation or the exit condition.