0% found this document useful (0 votes)
37 views

Discrete Mathematics and Its Applications, 8th Edition

The document contains a series of procedures and algorithms related to mathematical operations, including gcd, multiplication, and squaring of numbers. It discusses both recursive and iterative approaches for these operations, providing examples and explanations for each method. The content appears to be instructional, likely intended for a programming or mathematics course.

Uploaded by

Wakil Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Discrete Mathematics and Its Applications, 8th Edition

The document contains a series of procedures and algorithms related to mathematical operations, including gcd, multiplication, and squaring of numbers. It discusses both recursive and iterative approaches for these operations, providing examples and explanations for each method. The content appears to be instructional, likely intended for a programming or mathematics course.

Uploaded by

Wakil Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1118

S-38 Answers to Odd-Numbered Exercises

else return =y
x :

(n ⋅ modfactorial(n * 1, m)) mod m =z


y :

15. procedure gcd(a, b: nonnegative integers) z := w

fa < b assumed to holdg return z fz is the nth term of the sequenceg


if a= 0 then return b
else if a = b * a then return a
35. We first give a recursive procedure and then an iterative

else if a < b * a then return gcd(a, b * a)


procedure.

else return gcd(b * a, a)


procedure r(n: nonnegative integer)

17. procedure multiply(x, y: nonnegative integers)


if n < 3 then return 2n + 1
if y = 0 then return 0 else return r(n * 1) ⋅ (r(n * 2)) ⋅ (r(n * 3)) 2 3

else if y is even then


procedure i(n: nonnegative integer)
⋅ multiply (x, y_2)
return 2

else return 2 ⋅ multiply (x, (y*1)_2) + x


if n = 0 then z := 1
19. We use strong induction on a. Basis step: If a = 0, we
else if n = 1 then z := 3
know that gcd(0, b) = b for all b > 0, and that is precisely else

what the if clause does. Inductive step: Fix k > 0, assume x :=1
the inductive hypothesis—that the algorithm works correctly y :=3
for all values of its first argument less than k—and consider z := 5

what happens with input (k, b), where k < b. Because k > 0, for i := 1 to n * 2

the else clause is executed, and the answer is whatever the w := z ⋅ y ⋅ x


2 3

algorithm gives as output for inputs (b mod k, k). Because


x := y
b mod k < k, the input pair is valid. By our inductive hy-
y := z
gcd(b mod k, k), which equals
pothesis, this output is in fact
z := w
gcd(k, b) by Lemma 1 in Section 4.3. 21. If n = 1, then
return z fz is the nth term of the sequenceg
nx = x, and the algorithm correctly returns x. Assume that
The iterative version is more efficient.
the algorithm correctly computes kx. To compute (k + 1)x it

recursively computes the product of k + 1 * 1 = k and x, 37. procedure reverse(w: bit string)

and then adds x. By the inductive hypothesis, it computes that n : = length(w)


product correctly, so the answer returned is kx + x = (k + 1)x, if n f 1 then return w
which is correct. else return

substr(w, n, n)reverse (substr (w, 1, n * 1))


23. procedure square(n: nonnegative integer)

if n= 0 then return 0 ^substr(w, a, b) is the substring of w consisting of


else return square (n * 1) + 2(n * 1) + 1
the symbols in the ath through bth positions`
Let P(n) be the statement that this algorithm correctly com-
2
putes n . Because 0
2
= 0, the algorithm works correctly
39. The procedure correctly gives the reversal of 휆 as 휆 (ba-
sis step), and because the reversal of a string consists of its
(using the if clause) if the input is 0. Assume that the algo-

rithm works correctly for input k. Then for input k + 1, it last character followed by the reversal of its first n * 1 char-
gives as output (because of the else clause) its output when acters (see Exercise 37 in Section 5.3), the algorithm behaves

the input is k, plus 2(k + 1 * 1) + 1. By the inductive correctly when n > 0 by the inductive hypothesis. 41. The
2
hypothesis, its output at k is k , so its output at k + 1 is algorithm implements the idea of Example 14 in Section 5.1.

k
2
+ 2(k + 1 * 1) + 1 = k
2
+ 2k + 1 = (k + 1) 2
, as desired. If n = 1 (basis step), place the one right triomino so that its

× 2 board. If n > 1,
n
25. n multiplications versus 2 27. O(log n) versus n
armpit corresponds to the hole in the 2

29. procedure a(n: nonnegative integer) then divide the board into four boards, each of size 2
* ×2 * ,
n 1 n 1

if n= 0 then return 1 notice which quarter the hole occurs in, position one right tri-
else if n= 1 then return 2 omino at the center of the board with its armpit in the quarter
else return a(n * 1) ⋅ a(n * 2)
where the missing square is (see Figure 7 in Section 5.1), and
31. Iterative
invoke the algorithm recursively four times—once on each of
*1 *1
33. procedure iterative(n: nonnegative integer) the 2
n
×2 n
boards, each of which has one square missing

if n= 0 then z := 1 (either because it was missing to begin with, or because it is


else if n = 1 then z := 2
covered by the central triomino).
else

x :=1 43. procedure A(m, n: nonnegative integers)

y :=2 if m = 0 then return 2n


z := 3 else if n = 0 then return 0
for i := 1 to n * 2 else if n = 1 then return 2

w := x + y + z else return A(m * 1, A(m, n * 1))

You might also like