diff --git a/chapter_4/exercises.md b/chapter_4/exercises.md index 2882c7b..554c32a 100644 --- a/chapter_4/exercises.md +++ b/chapter_4/exercises.md @@ -9161,3 +9161,236 @@ See Page 266. degrees? Explain. Omitted. + +--- + +**Exercise Set 4.10** + +Page 278 + +Find the value of $z$ when each of the algorithm segments in 1 and 2 is +executed. + +1. + +$i := 2\\ \text{\textbf{if }} (i > 3 \text{ or } i \leq 0)\\ \ \ \ \ \text{\textbf{then }} z := 1\\ \ \ \ \ \text{\textbf{else }} z := 0$ + +2. + +$i := 3\\ \text{\textbf{if }} (i \leq 3 \text{ or } i > 6)\\ \ \ \ \ \text{\textbf{then }} z := 2\\ \ \ \ \ \text{\textbf{else }} z := 0$ + +3. Consider the following algorithm segment: + +$\text{\textbf{if }} x \cdot y > 0 \text{\textbf{ then do }} y := 3 \cdot x\\ \ \ \ \ x := x + 1 \text{\textbf{end do}}\\ \ \ \ \ z := x \cdot y$ + +Find the value of $z$ if prior to execution $x$ and $y$ have the values given +below. + +a. $x = 2, y = 3$ + +b. $x = 1, y = 1$ + +Find the values of $a$ and $e$ after execution of the loops in 4 and 5 by first +making trace tables for them. + +4. + +$a := 2\\ \text{\textbf{for }} i := 1 \text{\textbf{ to }} 3\\ \ \ \ \ a:= 3a + 1\\ \text{\textbf{next }} i$ + +5. + +$e := 2, f := 0\\ \text{\textbf{for }} k := 1 \text{\textbf{ to }} 3\\ \ \ \ \ e := e \cdot k\\ \ \ \ \ f := e + f\\ \text{\textbf{next }} k$ + +Make a trace table to trace the action of Algorithm 4.10.1 for the input +variables given in 6 and 7. + +6. $a = 26, d = 7$ + +7. $a = 59, d = 13$ + +8. The following algorithm segment makes change; given an amount of money $A$ + between 1¢ and 99¢, it determines a breakdown of $A$ into quarters $(q)$, + dimes $(d)$, nickels $(n)$, and pennies $(p)$. + +$$ +q := A \text{div } 25 \\ +A := A \mod 25 \\ +d := A \text{div } 10 \\ +A := A \mod 10 \\ +n := A \text{div } 5 \\ +p := A \mod 5 +$$ + +a. Trace this algorithm segment for $A = 69$. + +b. Trace this algorithm segment for $A = 87$. + +Find the greatest common divisor of each of the pairs of integers in 9-12. (Use +any method you wish.) + +9. $27$ and $72$ + +10. $5$ and $9$ + +11. $7$ and $21$ + +12. $48$ and $54$ + +Use the Euclidean algorithm to hand-calculate the greatest common divisors of +each of the pairs of itnegers in 13-16. + +13. $1,188$ and $385$ + +14. $509$ and $1,177$ + +15. $832$ and $10,933$ + +16. $4,131$ and $2,431$ + +Make a trace table to trace the action of Algorithm 4.10.2 for the input +variables given in 17-19. + +17. $1,001$ and $871$ + +18. $5,859$ and $1,232$ + +19. $1,570$ and $488$ + +**Definition: Integers $a$ and $b$ are said to be **relatively prime** if, and +only if, their greatest common divisor is $1$. + +In 20 and 21 trace the action of Algorithm 4.10.2 to determine whether the +integers are relatively prime. + +20. $4,167$ and $2,563$ + +21. $34,391$ and $6,728$ + +22. Prove that for all positive integers $a$ and $b$, $a \mid b$ if, and only + if, $\text{gcd}(a, b) = a$. (Note that to prove "$A$ if, and only if, $B$," + you need to prove "if $A$ then $B$" and "if $B$ then $A$.") + +23. + +a. Prove that if $a$ and $b$ are integers, not both zero, and +$d = \text{gcd}(a, b)$, then $\dfrac{a}{d}$ and $\dfrac{b}{d}$ are integers with +no common divisor that is greater than $1$. + +b. Write an algorithm that accepts the numerator and denominator of a fraction +as input and produces as output the numerator and denominator of that fraction +written in lowest terms. (The algorithm may call upon the Euclidean algorithm as +needed.) + +24. Complete the proof of Lemma 4.10.2 by proving the following: If $a$ and $b$ + are any integers with $b \neq 0$ and $q$ and $r$ are any integers such that + +$$ a = bq + r $$ + +then + +$$ \text{gcd}(b, r) \leq \text{gcd}(a, b) $$ + +25. + +a. Prove: If $a$ and $d$ are positive integers and $q$ and $r$ are integers such +that $a = dq + r$ and $0 < r < d$, then + +$$ -a = d(-(q + 1)) + (d - r) $$ + +and + +$$ 0 < d - r < d $$ + +b. Indicate how to modify Algorithm 4.10.1 to allow for the input $a$ to be +negative. + +26. + +a. Prove that if $a$, $d$, $q$, and $r$ are integers such that $a = dq + r$ and +$0 \leq r < d$, then + +$$ q = \left\lfloor \frac{a}{d} \right\rfloor \quad \text{ and } r = a - \left\lfloor \frac{a}{d} \right\rfloor \cdot d$$ + +b. In a computer language with a built-in floor function, $\text{div}$ and +$\mod$ can be calculated as follows: + +$$ a \text{div } d = \left\lfoor \frac{a}{d} \right\rfloor \quad \text{ and } \quad a \mod d = a - \left\lfloor \frac{a}{d} \right\rfloor \cdot d $$ + +Rewrite the steps of Algorithm 4.10.2 for a computer language with a built-in +floor function but without $\text{div}$ and $\mod$. + +27. An alternative to the Euclidean algorithm uses subtraction rather than + division to compute greatest common divisors. (After all, division is + repeated subtraction.) It is based on the following lemma. + +**Lemma 4.10.3** + +**Algorithm 4.10.3 Computing gcd's by Subtraction** + +_[Given two positive integers $A$ and $B$, variables $a$ and $b$ are set equal +to $A$ and $B$. Then a repetitive process begins. If $a \neq 0$, and $b \neq 0$, +then the larger of $a$ and $b$ is set equal to +$a - b (\text{if } a \geq b) \text{ or to } b - a(\text{if } a < b)$, and the +smaller of $a$ and $b$ is left unchanged. This process is repeated over and over +until eventually $a$ or $b$ becomes $0$. By Lemma 4.10.3, after each repetition +of the process,_ + +$$ \text{gcd}(A, B) = \text{gcd}(a, b) $$ + +_After the last repetition,_ + +$$ \text{gcd}(A, B) = \text{gcd}(a, 0) \quad \text{ or } \quad \text{gcd}(A, B) = \text{gcd}(0, b) $$ + +_depending on whether $a$ or $b$ is nonzero. But by Lemma 4.10.1,_ + +$$ \text{gcd}(a, 0) = a \quad \text{ and } \quad \text{gcd}(0, b) = b $$ + +_Hence, after the last repetition,_ + +$$ \text{gcd}(A, B) = a \text{ if } a \neq 0 \quad \text{ or } \quad \text{gcd}(A, B) = b \text{ if } b \neq 0 $$ + +**Input:** $A, B$ _[positive integers]_ + +**Algorithm Body:** + +$a := A, b := B\\ \text{\textbf{while }} (a \neq 0 \text{ and } b \neq 0)\\ \ \ \ \ \text{\textbf{if }} a \geq b \text{\textbf{ then }} a := a - b\\ \ \ \ \ \ \ \ \ \text{\textbf{else }} b := b - a\\ \text{\textbf{end while}}\\ \ \ \ \ \text{\textbf{if }} a = 0 \text{\textbf{ then }} gcd := b\\ \ \ \ \ \text{\textbf{else }} gcd := a$ + +_[After execution of the **if-then-else** statement, +$\text{gcd} = \text{gcd}(A, B)$.]_ + +**Output:** $\text{gcd}$ _[a positive integer]_ + +a. Prove Lemma 4.10.3. + +b. Trace the execution of Algorithm 4.10.3 for $A = 360$ and $B = 336$. + +c. Trace the execution of Algorithm 4.10.3 for $A = 768$ and $B = 348$. + +Exercises 28-32 refer to the following definition. + +**Definition:** The **least common multiple** of two nonzero integers $a$ and +$b$, denoted $\text{\textbf{lcm}}(a, b)$, is the positive integer $c$ such that + +a. $a \mid c$ and $b \mid c$ + +b. for all positive integers $m$, if $a \mid m$ and $b \mid m$, then $c \leq m$. + +28. Find + +a. $\text{lcm}(12, 18)$ + +b. $\text{lcm}(2^2 \cdot 3 \cdot 5, 2^3 \cdot 3^2)$ + +c. $\text{lcm}(2800, 6125)$ + +29. Prove that for all positive integers $a$ and $b$, + $\text{gcd}(a, b) = \text{lcm}(a, b)$ if, and only if, $a = b$. + +30. Prove that for all positive integers $a$ and $b$, $a \mid b$ if, and only + if, $\text{lcm}(a, b) = b$. + +31. Prove that for all integers $a$ and $b$, + $\text{gcd}(a, b) \mid \text{lcm}(a, b)$. + +32. Prove that for all positive integers $a$ and $b$, + $\text{gcd}(a, b) \cdot \text{lcm}(a, b) = ab$. diff --git a/chapter_4/notes.md b/chapter_4/notes.md index 3f2f165..e1d1d6e 100644 --- a/chapter_4/notes.md +++ b/chapter_4/notes.md @@ -1242,3 +1242,252 @@ such a way that 3. there is no edge from any one vertex of $V$ to any other vertex of $V$; 4. there is no edge from any one vertex of $W$ to any other vertex of $W$. + +--- + +Page 268 + +Execution of an **if-then-else** statement occurs as follows: + +1. The _condition_ is evaluated by substituting the current values of all + algorithm variables appearing in it and evaluating the truth or falsity of + the resulting statement. + +2. If _condition_ is true, then $s_1$ is executed and execution moves to the + next algorithm statement following the **if-then-else** statement. + +3. If _condition_ is false, then $s_2$ is executed and execution moves to the + next algorithm statement following the **if-then-else** statement. + +--- + +Page 269 + +Execution of a **while** loop occurs as follows: + +1. The _condition_ is evaluated by substituting the current values of all the + algorithm variables and evaluating the truth or falsity of the resulting + statement. + +2. If _condition_ is true, all the statements in the body of the loop are + executed in order. Then execution moves back to the beginning of the loop and + the process repeats. + +3. If _condition_ is false, execution passes to the next algorithm statement + following the loop. + +--- + +Page 270 + +A **for-next** loop is executed as follows: + +1. The **for-next** loop _variable_ is set equal to the value of _initial + expression_. + +2. A check is made to determine whether the value of _variable_ is less than or + equal to the value of _final expression_. + +3. If the value of _variable_ is less than or equal to the value of _final + expression_, then the statements in the body of the loop are executed in + order, _variable_ is increased by $1$, and execution returns back to step 2. + +4. If the value of _variable_ is greater than the value of _final expression_, + then execution passes to the next algorithm statement following the loop. + +--- + +Page 272 + +**Algorithm 4.10.1 Division Algorithm** + +_[Given a nonnegative integer $a$ and a positive integer $d$, the aim of the +algorithm is to find integers $q$ and $r$ that satisfy the conditions +$a = dq + r$ and $0 \leq r < d$. This is done by subtracting $d$ repeatedly from +$a$ until the result is less than $d$ but is still nonnegative._ + +$$ 0 \leq a - d - d - d - \dots - d = a - dq < d$$ + +_The total number of $d$'s that are subtracted is the quotient $q$. The quantity +$a - dq$ equals the remainder $r$.]_ + +**Input:** _$a$ [a nonnegative integer], $d$ [a positive integer]_ + +**Algorithm Body:** + +$$ r:= a, q := 0 $$ + +_[Repeatedly subtract $d$ from $r$ until a number less than $d$ is obtained. Add +$1$ to $q$ each time $d$ is subtracted.]_ + +$\text{\textbf{while}} (r \geq d)\\ \ \ \ \ r := r- d\\ \ \ \ \ q:= q + 1\\ \text{\textbf{end while}}$ + +_[After execution of the $\text{\textbf{while}}$ loop, $a = dq + r$.]_ + +**Output:** $q$, $r$ _[nonnegative integers]_ + +--- + +Page 273 + +**Definition** + +Let $a$ and $b$ be integers that are not both zero. The **greatest common +divisor** of $a$ and $b$, denoted $\text{\textbf{gcd}}(a, b)$, is that integer +$d$ with the following properties: + +1. $d$ is a common divisor of both $a$ and $b$. In other words, + +$$ d \mid a \quad \text{ and } \quad d \mid b $$ + +2. For every integer $c$, if $c$ is a common divisor of both $a$ and $b$, then + $c$ is less than or equal to $d$. In other words, + +$$ \text{for every integer } c, \text{ if } c \mid a \text{ and } c \mid b \text{ then } c \leq d$$ + +--- + +Page 274 + +**Lemma 4.10.1** + +If $r$ is a positive integer, then $\text{gcd}(r, 0) = r$. + +**Proof:** Suppose $r$ is a positive integer. _[We must show that the greatest +common divisor of both $r$ and $0$ is $r$.]_ Certainly, $r$ is a common divisor +of both $r$ and $0$ because $r$ divides itself and also $r$ divides $0$ (since +every positive integer divides $0$). Also no integer larger than $r$ can be a +common divisor of $r$ and $0$ (since no integer larger than $r$ can divide $r$). +Hence $r$ is the greatest common divisor of $r$ and $0$. + +--- + +Page 274 + +**Lemma 4.10.2** + +If $a$ and $b$ are any integers not both zero, and if $q$ and $r$ are any +integers such that + +$$ a = bq + r $$ + +then + +$$ \text{gcd}(a, b) = \text{gcd}(b, r) $$ + +**Proof:** _[The proof is divided into two sections: (1) proof that +$\text{gcd}(a, b) \leq \text{gcd}(b, r)$, and (2) proof that +$\text{gcd}(b, r) \leq \text{gcd}(a, b)$. Since each $\text{gcd}$ is less than +or equal to the other, the two must be equal.]_ + +1. $\text{\textbf{gcd}}(a, b) \leq \text{\textbf{gcd}}(b, r)$: + +a. _[We will first show that any common divisor of $a$ and $b$ is also a common +divisor of $b$ and $r$.]_ + +Let $a$ and $b$ be integers, not both zero, and let $c$ be a common divisor of +$a$ and $b$. Then $c \mid a$ and $c \mid b$, and so, by definition of +divisibility, $a = nc$ and $b = mc$, for some integers $n$ and $m$. Substitute +into the equation + +$$ a = bq + r $$ + +to obtain + +$$ nc = (mc)q + r $$ + +Then solve for $r$: + +$$ r = nc - (mc)q = (n - mq)c $$ + +Now $n - mq$ is an integer, and so, by definition of divisibility, $c \mid r$. +Because we already know that $c \mid b$, we can conclude that $c$ is a common +divisor of $b$ and $r$ _[as was to be shown]_. + +b. _[Next we show that $\text{gcd}(a, b) \leq \text{gcd}(b, r)$.]_ + +Now the greatest common divisor of $a$ and $b$ defined because $a$ and $b$ are +not both zero. Also, by part (a), every common divisor of $a$ and $b$ is a +common divisor of $b$ and $r$, and so the greatest common divisor of $a$ and $b$ +is a common divisor of $b$ and $r$. But then $\text{gcd}(a, b)$ (being one of +the common divisors of $b$ and $r$) is less than or equal to the greatest common +divisor of $b$ and $r$: + +$$ \text{gcd}(a, b) \leq \text{gcd}(b, r) $$ + +2. $\text{\textbf{gcd}}(b, r) \leq \text{\textbf{gcd}}(a, b)$: + +The second part of the proof is very similar to the first part. It is left as an +exercise. + +--- + +Page 275 + +**Euclidean Algorithm Description** + +1. Let $A$ and $B$ be integers with $A > B \geq 0$. + +2. To find the greatest common divisor of $A$ and $B$, first check whether + $B = 0$. If it is, then $\text{gcd}(A, B) = A$ by Lemma 4.10.1. If it isn't, + then $B > 0$ and the quotient-remainder theorem can be used to divide $A$ by + $B$ to obtain a quotient $q$ and a remainder $r$: + +$$ A = Bq + r \quad \text{ where } \quad 0 \leq r < B $$ + +By Lemma 4.10.2, $\text{gcd}(A, B) = \text{gcd}(B, r)$. Thus the problem of +finding the greatest common divisor of $A$ and $B$ is reduced to the problem of +finding the greatest common divisor of $B$ and $r$. + +_[What makes this information useful is the fact that the larger number of the +pair $(B, r)$ is smaller than the larger number of the pair $(A, B)$. The reason +is that the value of $r$ found by the quotient-remainder theorem satisfies_ + +$$ 0 \leq r < B $$ + +_And, since by assumption $B < A$, we have that_ + +$$ 0 \leq r < B < A $$ + +_]_. + +3. Now just repeat the process, starting again at (2), but use $B$ instead of + $A$ and $r$ instead of $B$. The repetitions are guaranteed to terminate + eventually with $r = 0$ because each new remainder is less than the preceding + one and all are nonnegative. + +--- + +Page 277 + +**Algorithm 4.10.2 Euclidean Algorithm** + +_[Given two integers $A$ and $B$ with $A > B \geq 0$, this algorithm computes +$\text{gcd}(A, B)$. It is based on two facts:_ + +1. $\text{gcd}(a, b) = \text{gcd}(b, r)$ _if $a$, $b$, $q$, and $r$ are integers + with $b = b \cdot q + r$ and $0 \leq r < b$._ + +2. $\text{gcd}(a, 0) = a$._]_ + +**Input:** $A, B$ _[integers with $A > B \geq 0$]_ + +**Algorithm Body:** + +$a := A, b := B, r:= B$ + +_[If $b \neq 0$, compute $a \mod b$, the remainder of the integer division of +$a$ by $b$, and set $r$ equal to this value. Then repeat the process using $b$ +in place of $a$ and $r$ in place of $b$.]_ + +$\text{\textbf{while }} (b \neq 0)\\ \ \ \ \ r:= a \mod b$ + +_[The value of $a \mod b$ can be obtained by calling the division algorithm.]_ + +$\ \ \ \ a := b\\ \ \ \ \ b := r\\ \text{\textbf{end while}}$ + +_[After execution of the $\text{\textbf{while}}$ loop, $\text{gcd}(A, B) = a$.]_ + +$\text{gcd} := a$ + +**Output:** $\text{gcd}$ _[a positive integer]_ diff --git a/chapter_4/test_yourself.md b/chapter_4/test_yourself.md index 0c3570d..93d18f1 100644 --- a/chapter_4/test_yourself.md +++ b/chapter_4/test_yourself.md @@ -285,3 +285,56 @@ for each pair of vertices any one vertex of $W$ to any other vertex of $W$. one edge; no edge; no edge + +--- + +**Test Yourself** + +Page 277 + +1. When an algorithm statement of the form $x := e$ is executed, ______. + +2. Consider an algorithm statement of the following form. + +$\text{\textbf{if }(condition)}\\ \text{\textbf{then }} s_1\\ \text{\textbf{else }} s_2$ + +When such a statement is executed, the truth or falsity of the _condition_ is +evaluated. If _condition_ is true, ______. If _condition_ is false, ______. + +3. Consider an algorithm statement of the following form. + +$\text{\textbf{while }(condition)}$ + +_[statements that make up the body of the loop]_ + +$\text{\textbf{end while}}$ + +When such a statement is executed, the truth or falsity of the _condition_ is +evaluated. If _condition_ is true, ______. If _condition_ is false, ______. + +4. Consider an algorithm statement of the following form. + +$\text{\textbf{for } variable } := \text{initial expression \textbf{to} final expression.}$ + +_[statements that make up the body of the loop]_ + +$\text{\textbf{next } (same) variable}$ + +When such a statement is executed, _variable_ is set equal to the value of the +_initial expression_, and a check is made to determine whether the value of +_variable_ is less than or equal to the value of _final expression_. If so, +______. If not, ______. + +5. Given a nonnegative integer $a$ and a positive integer $d$ the division + algorithm computes ______. + +6. Given integers $a$ and $b$, not both zero, $\text{gcd}(a, b)$ is the integer + $d$ that satisfies the following two conditions: ______ and ______. + +7. If $r$ is a positive integer, then $gcd(r, 0) =$ ______. + +8. If $a$ and $b$ are integers not both zero and if $q$ and $r$ are nonnegative + integers such that $a = bq + r$ then $\text{gcd}(a,b ) =$ ______. + +9. Given positive integers $A$ and $B$ with $A > B$, the Euclidean algorithm + computes.