From a4ee4fe4d3a6a2c27bdb0cfb1c70a1d49a6f24fa Mon Sep 17 00:00:00 2001 From: tomit4 Date: Sun, 28 Jun 2026 16:44:40 -0700 Subject: [PATCH] :construction: Setup for 5.5 --- chapter_5/exercises.md | 153 +++++++++++++++++++++++++++++++++++++ chapter_5/notes.md | 61 +++++++++++++++ chapter_5/test_yourself.md | 31 ++++++++ 3 files changed, 245 insertions(+) diff --git a/chapter_5/exercises.md b/chapter_5/exercises.md index 38e99ce..eab95fa 100644 --- a/chapter_5/exercises.md +++ b/chapter_5/exercises.md @@ -6627,3 +6627,156 @@ Omitted. well-ordering principle for the integers. Omitted. + +--- + +**Exercise Set 5.5** + +Page 346 + +Exercises 1-5 contain a while loop and a predicate. In each case show that if +the predicate is true before entry to the loop, then it is also true after exit +from the loop. + +1. + +loop: + +$\text{\textbf{while}} (m \geq 0 \text{ and } m \leq 100)\\ \ \ \ \ m := m + 1\\ \ \ \ \ n := n - 1\\ \text{\textbf{end while}}$ + +predicate: $m + n = 100$ + +2. + +loop: + +$\text{\textbf{while}} (m \geq 0 \text{ and } m \leq 100)\\ \ \ \ \ m := m + 4\\ \ \ \ \ n := n - 2\\ \text{\textbf{end while}}$ + +predicate: $m + n \text{ is odd}$ + +3. + +loop: + +$\text{\textbf{while}} (m \geq 0 \text{ and } m \leq 100)\\ \ \ \ \ m := 3 \cdot m\\ \ \ \ \ n := 5 \cdot n\\ \text{\textbf{end while}}$ + +predicate: $m^3 > n^2$ + +4. + +loop: + +$\text{\textbf{while}} (n \geq 0 \text{ and } n \leq 100)\\ \ \ \ \ n := n + 1\\ \text{\textbf{end while}}$ + +predicate: $2^n < (n + 2)!$ + +5. + +loop: + +$\text{\textbf{while}} (n \geq 3 \text{ and } n \leq 100)\\ \ \ \ \ n := n + 1\\ \text{\textbf{end while}}$ + +predicate: $2n + 1 \leq 2^n$ + +Exercises 6-9 each contain a while loop annotated with a pre-and a +post-condition and also a loop invariant. In each case, use the loop invariant +theorem to prove the correctness of the loop with respect to the pre-and +post-conditions. + +6. _[Pre-condition: $m$ is a nonnegative integer, $x$ is a real number, $i = 0$, + and $\text{exp} = 1$.]_ + +$\text{\textbf{while}} (i \neq m)\\ \ \ \ \ 1. \text{exp} := \text{exp} \cdot x\\ \ \ \ \ 2. i := i + 1\\ \text{\textbf{end while}}$ + +_[Post-condition: $\text{exp} = x^m$]_ + +loop invariant: $I(n)$ is "$\text{exp} = x^n$ and $i = n$." + +7. _[Pre-condition: $\text{largest} = A[1]$ and $i = 1$]_ + +$\text{\textbf{while}} (i \neq m)\\ \ \ \ \ 1. i := i + 1\\ \ \ \ \ 2. \text{\textbf{if}} A[i] > \text{largest \textbf{then } \text{largest}} := A[i]\\ \text{\textbf{end while}}$ + +_[Post-condition: +$\text{largest} = \text{maximum value of } A[1], A[2], \dots, A[m]$]_ + +loop invariant: $I(n)$ is +"$\text{largest} = \text{maximum value of } A[1], A[2], \dots, A[n + 1]$ and +$i = n + 1$." + +8. _[Pre-condition: $\text{sum} = A[1]$ and $i = 1$]_ + +$\text{\textbf{while}} (i \neq m)\\ \ \ \ \ 1. i := i + 1\\ \ \ \ \ 2. \text{sum} := \text{sum} + A[i]\\ \text{\textbf{end while}}$ + +_[Post condition: $\text{sum} = A[1] + A[2] + \dots + A[m]$]_ + +loop invariant: $I(n)$ is "$i = n + 1$ and +$\text{sum} = A[1] + A[2] + \dots + A[n + 1]$." + +9. _[Pre-condition: $a = A$ and $A$ is a positive integer.]_ + +$\text{\textbf{while}} (a > 0)\\ \ \ \ \ a := a - 2\\ \text{\textbf{end while}}$ + +_[Post-condition: $a = 0$ if $A$ is even and $a = -1$ if $A$ is odd.]_ + +loop invariant: $I(n)$ is "Both $a$ and $A$ are even integers or both are odd +integers and, in either case, $a \geq -1$." + +10. Prove correctness of the **while** loop of Algorithm 4.10.3 (in exercise 27 + of Exercise Set 4.10) with respect to the following pre- and + post-conditions: + +_Pre-condition:_ $A$ and $B$ are positive integers, $a = A$, and $b = B$. + +_Post-condition:_ One of $a$ or $b$ is zero and the other is nonzero. Whichever +is nonzero equals $\text{gcd}(A, B)$. + +Use the loop invariant + +$I(n)$ + +"(1) $a$ and $b$ are nonnegative integers with +$\text{gcd}(a, b) = \text{gcd}(A, B)$, + +(2) at most one of $a$ and $b$ equals $0$, + +(3) $0 \leq a + b \leq A + B - n$." + +11. The following **while** loop implements a way to multiply two numbers that + was developed by the ancient Egyptians. + +_[Pre-condition: $A$ and $B$ are positive integers, $x = A$, $y = B$, and +$\text{product} = 0$.]_ + +$\text{\textbf{while}} (y \neq 0)\\ \ \ \ \ r := y \mod 2\\ \ \ \ \ \text{\textbf{if }} r = 0\\ \ \ \ \ \ \ \ \ \text{\textbf{then do }}\\ \ \ \ \ \ \ \ \ \ \ \ \ x := 2 \cdot x\\ \ \ \ \ \ \ \ \ \ \ \ \ y := y \text{ div } 2\\ \ \ \ \ \ \ \ \ \text{\textbf{end do}}\\ \ \ \ \ \text{\textbf{if }} r = 1\\ \ \ \ \ \ \ \ \ \text{\textbf{then do }}\\ \ \ \ \ \ \ \ \ \ \ \ \ \text{product} := \text{product } + x\\ \ \ \ \ \ \ \ \ \ \ \ \ y := y - 1\\ \ \ \ \ \ \ \ \ \text{\textbf{end do}}\\ \text{\textbf{end while}}$ + +_[Post-condition: $\text{product } = A \cdot B$]_ + +a. Make a trace table to show that the algorithm gives the correct answer for +multiplying $A = 13 \text{ times } B = 18$. + +b. Prove the correctness of this loop with respect to its pre-and +post-conditions by using the loop invariant + +$I(n)$: "$xy + \text{ product} = A \cdot B$" + +12. The following sentence could be added to the loop invariant for the + Euclidean algorithm: + +There exist integers $u$, $v$, $s$, and $t$ such that $a = uA + vB$ and +$b = sA + tB$. + +a. Show that this sentence is a loop invariant for + +$\text{\textbf{while}} (b \neq 0)\\ \ \ \ \ r := a \mod b\\ \ \ \ \ a := b\\ \ \ \ \ b := r\\ \text{\textbf{end while}}$ + +b. Show that if initially $a = A$ and $b = B$, then sentence (5.5.12) is true +before the first iteration of the loop. + +c. Explain how the correctness proof for the Euclidean algorithm together with +the results of (a) and (b) above allow you to conclude that given any integers +$A$ and $B$ with $A > B \geq 0$, there exist integers $u$ and $v$ so that +$\text{gcd}(A, B) = uA + vB$. + +d. By actually calculating $u$, $v$, $s$, and $t$ at each stage of execution of +the Euclidean algorithm, find integers $u$ and $v$ so that +$\text{gcd}(330, 156) = 330u + 156v$. diff --git a/chapter_5/notes.md b/chapter_5/notes.md index c7b93ce..8310f3e 100644 --- a/chapter_5/notes.md +++ b/chapter_5/notes.md @@ -908,3 +908,64 @@ The preceding arguments prove that there exists integers $r$ and $q$ for which $$ n = dq + r \text{ and } 0 \leq r < d $$ _[as was to be shown.]_ + +--- + +Page 339 + +**Definition** + +A loop is defined as **correct with respect to its pre- and post-conditions** +if, and only if, whenever the algorithm variables satisfy the pre-condition for +the loop and the loop terminates after a finite number of steps, the algorithm +variables satisfy the post-condition for the loop. + +--- + +Page 340 + +**Theorem 5.5.1 Loop Invariant Theorem** + +Let a **while** loop with guard $G$ be given, together with pre- and +post-conditions that are predicates in the algorithm variables. Also let a +predicate $I(n)$, called the **loop invariant**, be given. If the following four +properties are true, then the loop is correct with respect to its pre- and +post-conditions. + +**I. Basis Property:** The pre-condition for the loop implies that $I(0)$ is +true before the first iteration of the loop. + +**II. Inductive Property:** For every integer $k \geq 0$, if the guard $G$ and +the loop invariant $I(k)$ are both true before an iteration of the loop, then +$I(k + 1)$ is true after an iteration of the loop. + +**III. Eventual Falsity of Guard:** After a finite number of iterations of the +loop, the guard $G$ becomes false. + +**IV. Correctness of the Post-Condition:** If $N$ is the least number of +iterations after which $G$ is false and $I(N)$ is true, then the values of the +algorithm variables will be as specified in the post-condition of the loop. + +**Proof:** + +The loop invariant theorem follows easily from the principle of mathematical +induction. Assume that $I(n)$ is a predicate that satisfies properties I-IV of +the loop invariant theorem. _[We will prove that the loop is correct with +respect to its pre- and post-conditions.]_ Properties I and II are the basis and +inductive steps needed to prove the truth of the following statement: + +For every integer $n \geq 0$, if the **while** loop iterates $n$ times, then +$I(n)$ is true. + +Thus, by the principle of mathematical induction, since both I and II are true, +statement (5.5.1) is also true. + +Property III says that the guard $G$ eventually becomes false. At that point the +loop will have been iterated some number, say $N$, of times. Since $I(n)$ is +true after the $n$th iteration for every $n \geq 0$, then $I(n)$ is true after +the $N$th iteration. That is, after the $N$th iteration the guard is false and +$I(N)$ is true. But this is the hypothesis of property IV, which is an if-then +statement. Since statement IV is true (by assumption) and its hypothesis is true +(by the argument just given), it follows (by modus ponens) that its conclusion +is also true. That is, the values of all algorithm variables after execution of +the loop are as specified in the post-condition for the loop. diff --git a/chapter_5/test_yourself.md b/chapter_5/test_yourself.md index 24528da..94a065f 100644 --- a/chapter_5/test_yourself.md +++ b/chapter_5/test_yourself.md @@ -96,3 +96,34 @@ $a$; $k$; $P(k + 1)$ than or equal to every _____, then _____. one integer; integer in $S$; $S$ contains a least element. + +--- + +**Test Yourself** + +Page 346 + +1. A pre-condition for an algorithm is _____ and a post-condition for an + algorithm is _____. + +2. A loop is defined as correct with respect to its pre- and post-conditions if, + and only if, whenever the algorithm variables satisfy the pre-condition for + the loop and the loop terminates after a finite number of steps, then _____. + +3. For each iteration of a loop, if a loop invariant is true before iteration of + the loop, then _____. + +4. Given a **while** loop with guard $G$ and a predicate $I(n)$ if the following + four properties are true, then the loop is correct with respect to its pre- + and post-conditions: + +(a) The pre-condition for the loop implies that _____ before the first iteration +of the loop. + +(b) For every integer $k \geq 0$, if the guard $G$ and the predicate $I(k)$ are +both true before an iteration of the loop, then _____. + +\(c\) After a finite number of iterations of the loop, _____. + +(d) If $N$ is the least number of iterations after which $G$ is false and $I(N)$ +is true, then the values of the algorithm variables will be as specified _____.