Discrete Math Quiz 6 (4.6-4.11)
4.11: L-shaped tiles A 5 by 5 square, with the middle square missing, can be tiled with L-shaped trominoes.
TRUE (correct) Rationale: We can make a 2 by 3 rectangle with two L-shaped trominoes. Four of these rectangles can cover the given square, with the long side of one of the rectangles along each side.
4.7: A recursive string operation Let Σ = {0,...,9} and consider the following function min from Σ* to Σ*: (1) min(λ) = λ (2) if w = λ, then min(wa) = a for any letter a (3) if w ≠ λ, then min(wa) is the smaller of the letters min(w) and a, for any letter a. Then for any nonempty string u and any string v, min(uv) ≤ min(u).
TRUE (correct) Rationale: We can prove this by induction on all strings v. For the base case, min(uλ) = min(u). In the inductive case, appending a letter either decreases the value of the function or keeps it the same. So if min(uv) is already bounded above by min(u), min(uva) is bounded above by min(u) as well.
4.7: A property of strings Define the property Q of binary strings so that Q(λ) is true, and for any string w, Q(w0) is true if and only if Q(w) is, and Q(w1) is true if and only if Q(w) is false. Then Q does not satisfy the property that for any strings u and v, Q(uv) is the exclusive or of Q(u) and Q(v).
TRUE (correct) Rationale: Q(w) is true if and only if w has an even number of ones in it. If u and v each satisfy P, uv does as well, whereas the exclusive or of two 1's would be a 0. The negation of Q(w) satisfies this property.
4.11: L-shaped tiles Let n be any odd natural that is not divisible by 3. Then an n by n square figure, with the one middle square missing, can be covered with L-shaped tiles.
TRUE (correct) Rationale: The 1 by 1 square with its middle square missing can be covered with no tiles. The 5 by 5 square with its middle square missing can be covered using four 3 by 2 rectangles, each of which can be made from two L-shaped tiles. If I have already covered a k by k square with its middle square missing, I can extend this to a k+6 by k+6 square (still with middle square missing) by using four 3 by k+3 rectangles. (If k is odd, I can break the 3 by k+3 rectangles each into 3 by 2 rectangles.) P(1), P(5), and "for all x: P(x) → P(x+6)" suffice to prove P(x) for all odd x not divisible by 3.
4.7: The Suffix Operation Recall that if x and y are strings, x is a suffix of y if there exists a string z such that zx = y. Then the following method correctly determines whether x is a suffix of y: public boolean isSuffix (string x, string y) { if (isEmpty(x)) return true; if (isEmpty(y)) return false; if (last(x) != last(y)) return false; return isSuffix(allButLast(x), allButLast(y));}
TRUE (correct) Rationale: The empty string is a suffix of any string. If we reach line 3 with y empty, x is not empty and so is not a suffix of y. If both strings are nonempty and the last letters differ, x is not a suffix of y. If the last letters are the same and we delete them, we don't change the answer of the suffix question.
4.7: Divisibility by 3 The following pseudo-Java method takes a binary string as input and returns a natural that is not always the remainder when the natural represented in binary by the string is divided by 3. (See Excursion 4.8 for definitions of the mappings from binary strings to naturals and back.) static natural mod3 (string w) { if (isEmpty(w)) return 0; natural x = mod3 (allButLast(w)); if (last(w) == '0') return x; return (x + 1) % 3;
TRUE (correct) Rationale: The function computes the sum of the bits in w, modulo 3. For decimal numbers this sum is congruent to the value of w, modulo 3, but this is not true for binary numbers. For example, mod3(11) will return 2, while the binary value of "11" is 3, which is congruent to 0 modulo 3. The correct rule for binary divisibility by 3 is like the rule for divisibility by 11 in decimal -- we need the alternating sum of the digits.
4.6: An addition proof Suppose I want to prove "for all x: S0 + x = Sx". Then the following is valid: "Ordinary induction on all naturals x. Base case: x = 0, the desired formula becomes "S0 + 0 = S0". This is true by the base case of the definition of addition. Inductive hypothesis: S0 + x = Sx. Inductive goal: S0 + Sx = S(Sx). Proof of inductive step: By the inductive case of the definition of addition, S(S0 + x) = S0 + Sx. By the inductive hypothesis, applied inside the parentheses, the left-hand side of this equation is equal to S(Sx)). So the inductive goal is true."
TRUE (correct) Rationale: This is a completely valid proof.
4.6: Doubling a number The following is a valid proof of "for all x: SS0 · x = x + x": "Ordinary induction on all naturals x. Base case: SS0 · 0 = 0 + 0. True since the left-hand side equals 0 by the base case of the definition of multiplication, and the right-hand side equals 0 by the base case of the definition of addition. Inductive hypothesis: SS0 · x = x + x. Inductive goal: SS0 · Sx = Sx + Sx. Proof of inductive step: We begin with SS0 · Sx, which by the inductive case of the definition of multiplication is (SS0 · x) + SS0. By the inductive hypothesis this is (x + x) + SS0. By the inductive case of the definition of addition, this is S(x + x) + S0, which is S(S(x + x)) by the same case again. Meanwhile Sx + Sx is S(Sx + x) by the definition of addition, which is S(x + Sx) by the Lemma in Section 4.6.2. Using the definition of addition inside the parentheses, this last expression is also S(S(x+x)) and the two sides have been shown equal."
TRUE (correct) Rationale: This is a completely valid proof.
4.11: Tiling With L-tetrominoes The L-tetromino has four squares, three in a row and the fourth attached to one of the ends of the row, off to the side. For any positive natural n, define Ln to be the figure with the shape of an L-tetromino, but n times as large, so that it contains 4n2 total squares. Here is a picture of L2. x x x x x x x x x x x x x x x x With this definition, if n = 2k for any natural k, then Ln can be tiled by L-tetrominoes.
TRUE (correct) Rationale: This is an easy induction on n. The base case has k = 0 and thus n = 1. Since L1 is exactly an L-tetromino, we can tile it with one L-tetromino. For the inductive case, we assume that we can tile Ln and prove that we can tile L2n. We can divide L2n into four copies of Ln as follows: 1 1 3 3 3 4 1 2 3 4 4 4 1 2 2 2 Using the IH to tile each of the four copies, we tile the entire L2n.
4.6: A function on naturals Define the function f from naturals to naturals as follows. We define f(0) to be 1, and for all naturals x we define f(S(x)) to be 1 - f(x), where S is the successor operation and the minus operator is the one defined in Exercise 4.6.3. Then for any natural x, f(x) = 1 if x is even and f(x) = 0 if x is odd.
TRUE (correct) Rationale: This is correct -- we can prove by induction on all naturals x that the function value f(x) is defined and equal to the specified value of 0 or 1.
4.7: One's complement and concatenation The one's complement operator oc is defined in Exercise 4.7.3. Then this operator is linear over concatenation, so that the statement oc(uv) = oc(u)oc(v) is true for any binary strings u and v.
TRUE (correct) Rationale: This is true, and easily proved by induction on all strings v.
4.7: A prefix operation Recall that a string x is a suffix of a string y if there exists a string z such that zx = y. Then the following pseudo-Java code determines whether its first input is a suffix of its second: static boolean suffix (string x, string y) { if (isEmpty(x)) return true; if (isEmpty(y)) return false; if (last(x) != last(y)) return false; return suffix (allButLast(x), allButLast(y));}
TRUE (correct) Rationale: This works: λ is a suffix of any string, no nonempty string is a suffix of λ, one nonempty string can only be the suffix of another if their last letters match, and if their last letters do match, we can remove them both without changing the answer to the suffix question.
4.7: Prefix definition The following predicate prefix(x, y) as given below is a valid definition of the relation "string x is a prefix of y". We define prefix(x, y) to be true if and only if it follows from repeated applications of rules 1 and 2: (1) for all x, prefix(x, x) = true for all x (2) for all y ≠ λ, prefix(x, y) if prefix(x, allButLast(y)).
TRUE (correct) Rationale: Yes, Case 1 is the base case: x is the shortest string for which x is a prefix. Case 2 tells us that extending x by arbitrary characters will still yield a string of which x is a prefix.
4.11: EA on Fibonacci F(n+3) Recall that the Fibonacci numbers are defined by F(0) = 0, F(1) = 1, and F(n+1) = F(n) + F(n-1). If I start the Euclidean Algorithm with inputs F(n+3) and F(n), for any n, then it will terminate after about n/3 steps.
TRUE (correct) Rationale: Yes, by induction, since we can show that F(n+3) = 4F(n) + F(n-3).
4.11 Cutting Pizzas Suppose I have two convex pizzas (not overlapping) and I want to divide them into pieces with n straight cuts, without moving the pizzas from their original positions. Let P(n) be the maximum number of pieces I can create. Then P(0) = 2, P(1) = 4, P(2) = 7, and P(3) = 10.
FALSE (correct) Rationale: With no cuts we have two pieces, and one cut can divide both so that we have four. A second cut can only intersect the first cut within one of the pieces, so it cuts that piece into four and the other into at most three, so P(2) = 7 is also correct. With three cuts, we can have all three intersect within one pizza, making it into seven pieces, and then have each cut pass through the other pizza, making it into four. So P(3) is actually 11.
4.7: Reversing numbers Consider the following pseudo-Java methods for naturals: natural revDig2(natural x, natural r) { if (x == 0) return r; return revDig2(x / 10, 10*r + x % 10);} natural revDig(natural x) { return revDig2(x, 0); } Then for any natural x, revDig(revDig(x)) = x.
FALSE (correct) Rationale: revDig indeed reverses digits, but leading zeroes can't be distinguished in the result and thus trailing zeroes in the original number disappear. The claim is false for any multiples of 10, e.g., revDig(1200) = 21 and thus revDig(revDig(1200)) = 12
3.1 Hash Table Example Suppose I have a hash table with locations numbered {0, 1, 2,..., 90}. If I find a full location x, I next try location (x + 14) % 91. Then if there is any empty space in the table, I will eventually find it.
False Rationale: 14 and 91 are both divisible by 7, so if I started at a multiple of 7, I will only try multiples of 7 before reaching my start point again. The empty spaces might be elsewhere.
3.6: CRT feasible and infeasible systems Consider the system of three congruences x = a (mod 3), x = b (mod 5), x = c (mod 9). Then the number of triples (a, b, c) in {0, 1, 2} x {0, 1, 2, 3, 4} x { 0, 1, 2, 3, 4, 5, 6 ,7, 8 } for which the system has a solution is equal to the number of triples for which the system does not have a solution.
False Rationale: For every pair (b, c) there is a unique solution (mod 5 * 9) for the last two equations. If x = c (mod 9), then x = c (mod 3). Thus, for every pair (b, c) there is only one value of a that gives a solution, and two values for which the system is impossible. Note that since there are 3*5*9 = 135 triples involved here, it is impossible for the number with solutions to equal the number without solutions, as 135 is an odd number. So you could confidently answer "false" without analyzing the congruence system at all.
4.6: Subtraction As in Exercise 4.6.3, define the subtraction operator by the rules "x - 0 = x", "x - Sy = 0 if x - y = 0", and "x - Sy = pred(x - y) if x - y ≠ 0". Then this operator satisfies the equation "(x - y) + z = (z - y) + x" for any naturals x, y, and z.
FALSE (correct) Rationale: This operator only returns naturals, so that x - y = 0 if y ≥ x. If we try x = 4, y = 3, and z = 2, we get that (4 - 3) + 2 = 3 but (2 - 3) + 4 = 4.
4.11: A recursive function on naturals Define the following function h from naturals to naturals: h(0) = 1, if x is odd, then h(x) = 1 + h(x-1), and if x is even and positive, h(x) = 1 + h(x/2). Then for any natural x, h(x) is the number of ones in the binary string for x, plus the number of zeros in the binary string for x.
FALSE (correct) Rationale: It is twice the number of ones, plus the number of zeros.
4.6 Computing Division The following recursive pseudo-Java method always returns the natural given by the Java expression "a/b", given any natural a and any positive natural b. public natural slash (natural a, natural b) { if (b == 0) throw new ArithmeticException ("Divide by zero"); if (a <= b) return 0; return 1 + slash(a - b, b);}
FALSE (correct) Rationale: This would be correct if the "<=" on the third line were "<". As it is, slash(2, 2) would be 0 rather than the correct answer of 1.
4.7: Palindromes A string w is a palindrome if wR = w. Then the following recursive pseudo-Java method computes whether a string is a palindrome. static boolean pal(string w) { if (isEmpty(w)) return true; if (isEmpty(allButLast(w))) return true; if (last(w) != last(rev(w))) return false; return pal(allButLast(rev(allButLast(w))));}
TRUE (correct) Rationale: If a string has length 0 or 1, it is a palindrome. If the string has two or more letters, we return false if the first and last letter don't match, and recurse after stripping the first and last letter if they do match.
4.6: A function on naturals. Suppose we define a function f from naturals to naturals recursively as follows. We define f(0) to be 0, and for any natural x we define f(Sx) to be 1. Then this function satisfies the equation f(x · y) = f(x) · f(y), for any naturals x and y.
TRUE (correct) Rationale: If either x or y is 0, both sides evaluate to 0. If both x and y are positive, both sides evaluate to 1.
4.7: Second letter of a string The following pseudo-Java method returns the second letter of a string, if it has one. public char second (string w) { return last(allButLast(rev(w)));}
TRUE (correct) Rationale: If w has two or more letters, the second letter of w is the next to last letter of rev(w). If w has no letters, the call to allButLast will throw an exception. If w has exactly one letter, the call to last will throw an exception.
4.7 A String Induction Proof Let w be any string over the alphabet {0,1,2,3,4,5,6,7,8,9}. Then this string represents a natural in decimal notation, where the following method calculates the value: public natural value (string w) { if (isEmpty(w)) return 0; return 10 * value (allButLast(w)) + (last(w) - '0');} (Here we use Java's ability to treat characters as integers.) Suppose we want to prove that for any such string w, value(w) is congruent modulo 9 to the sum of w's digit values. Then the inductive case assumes that w has this property and proves that allButLast(w) has it as well.
FALSE (correct) Rationale: It should assume that allButLast(w) has it and prove that w has it.
4.6: A semiring? Let A be a finite set and let P be the power set of A, that is, the set {S: S is a subset of A}. For any two sets S and T, define "S + T" to be the union of S and T, and "S·T" to be the intersection of S and T. Then P forms a semiring where Ø is both the additive and multiplicative identity.
FALSE (correct) Rationale: P is a semiring under those operations, but the multiplicative identity is A, not Ø.
4.7: Definition of length. The following is a good definition of string length: (1) length(λ) = 0, (2) length(cat(u,v)) = length(u) + length(v) for all strings u, v.
FALSE (correct) Rationale: The defined length function is additive, but we don't know that length(a) = 1 for a single character. Any other value would work and produce a length in multiples of that value.
4.7: A substring method? Recall that a string u is a substring of a string v if there exists strings x and y such that v = xuy. Then the following pseudo-Java method correctly determines whether its first argument is a substring of its second. It uses the methods first and allButFirst defined in Problem 4.7.3. static boolean substring (string u, string v) { if (isEmpty(u)) return true; if (isEmpty(v)) return false; return (substring (u, allButFirst(v)) || substring (u, allButLast(v)));}
FALSE (correct) Rationale: The method never actually compares u to v. If we call substring("0", "0"), it will return false as it will compute the OR of substring("0", "") with itself. We need a line after the two if statements that returns true if u equals v.
4.6: Semiring Axioms Consider the set {1, 2, 3, 4, 5} with the following definition of "addition" and "multiplication". To compute x + y or x * y in our system, we first compute the same operation over the naturals, then map any numbers larger than 5 to 5. Thus 3 + 4, 2 * 3, and 5 * 5 are each equal to 5. Then this system satisfies the semiring axioms defined on page 4-30 of the textbook.
FALSE (correct) Rationale: There is no additive identity. If we added 0 to the system and kept the same rules, we would have a semiring, which we have called "rabbit arithmetic" elsewhere, following Richard Adams' novel Watership Down.
4.3: Number of Nonempty Binary Strings Let P(n) be the statement "there are an odd number of binary strings with length < n". Then following is a valid induction proof that P(n) is true for all naturals n: "Base case: The only string of length 0 is λ, so there is one string of length 0, and 1 is odd. Inductive hypothesis: "There are an odd number of binary strings with length < n." Inductive goal: "There are an odd number of binary strings with length < n+1." Proof of inductive step: It is proved in the text that there are exactly 2n binary strings of length n, an even number. The number of strings of length < n+1 is the number with length < n (odd, by the IH) plus the number with length exactly n (even), and this number is thus odd."
FALSE Rationale: The base case of P(0) actually should say that there are an odd number of strings with length < 0, which is false -- there are no strings with length < 0. The inductive step proof is also invalid when n = 0, since 20 is not even.
2.9 Composition is one-to-one? Let f be a function from A to B and let g be a function from B to C. If the composition g ° f (defined by (g ° f) (a) = g(f(a))) is one-to-one, then both f and g must be one-to-one.
FALSE Rationale: The first function applied (f) must be one-to-one, since otherwise, two distinct elements from A will be mapped onto one from B, which g will further map onto one element in C, and then g ° f would not be one-to-one. But g need not be one-to-one: consider A = {a, b}, B = {1, 2, 3} and C = {x, y} with f(a) = 1, f(b) = 2, g(1) = x, g(2) = g(3) = y. Since element 3 is not a value of f, g(3) has no bearing on the properties of g ° f.
4.11: Cutting Chicago Pizzas In Chicago, pizza is traditionally rectangular and divided by cuts in straight lines that are parallel to one of the sides. Following this rule, for any natural n, the maximum number of pieces I can make with n cuts is (n/2)2, if n is even, and (n2 - 1)/4 if n is odd.
FALSE (correct) Rationale: If n is even, our best method is to make n/2 cuts in each direction, which make (1 + n/2)2 total pieces. If n is odd, we should make (n-1)/2 cuts in one direction and (n+1)/2 in the other, which makes (n+1)(n+3)/4 total pieces.
4.6: The equality operation The equality operation on naturals is correctly computed by the following pseudo-Java method: boolean equals (natural x, natural y) { if (zero(x)) return zero(y); return equals(pred(x), pred(y));}
FALSE (correct) Rationale: If we call equals(1, 0) this code calls pred(0) and throws an exception. We should add a line if (zero(y)) return false; after the existing if statement.
4.11: EA on three-digit numbers If we start the EA with any two three-digit numbers, it will finish with at most twelve divisions.
FALSE (correct) Rationale: If we start with the consecutive Fibonacci numbers 987 and 610, the first division has remainder 377, the second 233, the third 144, followed by 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, and the fourteenth has remainder 0.
4.6 Bitwise XOR We define the "bitwise XOR" pseudo-function on naturals by the following code, using Java notation for integer division: public natural bitwiseXOR (natural a, natural b) { if (b == 0) return a; if ((a % 2) == (b % 2)) return 2 * bitwiseXOR(a/2, b/2); else return 1 + 2 * bitwiseXOR(a/2, b/2);} Then this function is commutative but not associative.
FALSE (correct) Rationale: It is both commutative and associative. As the name suggests, each bit of the binary representation of the result is the XOR of the corresponding bits of the input numbers. Since XOR is both commutative and associative on booleans, this function is both commutative and associative as well.
4.6: Exponentiation As in Problem 4.6.5, define the exponentiation function on naturals by the rules x0 = 1 and xS(y) = xy times x. Then this operation is associative but not commutative.
FALSE (correct) Rationale: It is not commutative or associative. If we write xy as E(x, y), associativity would say that E(x, E(y, z)) should be equal to E(E(x, y), z)). But if we take x = 2, y = 3, and z = 2, E(x, E(y, z)) = E(2, E(3, 2)) = E(2, 9) = 512, while on the other hand E(E(x, y), z) = E(E(2, 3), 2) = E(8, 2) = 64. It works if x = y = z = 2 and if one of the numbers is 0 or 1, but hardly ever else.
4.11: Cutting Cheese Suboptimally Suppose there is a point x in the middle of my block of cheese, and all of my n straight planar cuts must go through a single common point x. Then the number of pieces is smaller than the number without that restriction, but is still a cubic polynomial in n.
FALSE (correct) Rationale: It is smaller, but not a cubic. The pizza numbers with this restriction are 1, 2, 4, 6, 8,..., as with n cuts through the center we divide the pizza into 2n slices (as round pizzas are typically cut in practice). Again the cheese numbers are a sum of the pizza numbers and go 1, 2, 4, 8, 14, 22, 32,..., so that C'(n) = (n-1)2 + (n-1) + 2 for positive n. This is a quadratic function, not a cubic.
4.7 A string function Consider the following pseudo-Java method: public natural count (string w, char a) { if (isEmpty(w)) return 0; if (last(w) == a) return 1 + count(allButLast(w), a); return count(allButLast(w), a);} Then this method returns the number of occurrences of the letter a in the string w.
TRUE (correct) Rationale: It does, and we can prove this easily by induction on strings, because if the recursive call returns the number of a's in allButLast(w), the original call returns the number in w.
4.11: EA on Powers of 2 Suppose that a and b are each powers of 2, with a > b. Then the Euclidean Algorithm, on inputs a and b, will terminate after at most log2(a) steps.
TRUE (correct) Rationale: It will terminate after one step because b must divide a exactly. And since a is a power of 2 that is larger than another power of 2, it is at least 2 and its base-2 log is at least 1.
4.6 An addition and multiplication fact The statement "for all u: u × SS0 = (0 + u) + u" can be proved from the definitions of addition and multiplication, without using induction.
TRUE (correct) Rationale: Let u be arbitrary. u × SS0 = (u × S0) + u = ((u × 0) + u) + u = (0 + u) + u with the definitions alone. But simplifying to u + u would require the rule 0 + x = x, which we proved using induction.
4.7: A property of strings Let Σ = {0, 1}. Define a property P of strings as follows. P(λ) is true. For any string w and any letter a, P(wa) is true if and only if either w = λ, or both P(w) is true and last(w) = a. Then there exist strings u and v such that both P(u) and P(v) are true but P(uv) is false.
TRUE (correct) Rationale: P(w) is true if and only if every letter in w is the same. So P(0) and P(1) are both true, but P(01) is false.
4.11: Tiling with rectangles Let n be any odd natural. Suppose I can tile a 3n-2 by 3n-2 square, with the middle square missing, using 2 by 3 rectangles. Then I can also tile a 3n+2 by 3n+2 square, with the middle square missing, using 2 by 3 rectangles.
TRUE (correct) Rationale: Place n rectangles with their long sides along each of the four sides of the 3n+2 by 3n+2 square. This covers a band of width 2 along all four sides, leaving a 3n-2 by 3n-2 remainder.