University of California, San Diego Department of Computer Science and Engineering CSE 20 Solutions to Midterm Exam Winter 2018 Problem 1 (30 points) a. The boolean function f(p, q) = (q p) ( q p) is specified in: disjunctive normal form (DNF) conjunctive normal form (CNF) neither Solution: Disjunctive normal form is OR of minterms, which is exactly what we have here. b. The statement form (p q) (q p) is: a tautology a contradiction neither Solution: If p = T and q = T, the statement is true. But if p = F and q = T, the statement is false. Also, this statement says that an implication implies its converse, which is certainly false. c. Let C be the set of all cats, and let D be the set of all dogs. Let S be the statement: Every cat hates every dog. Which of the following statements is logically equivalent to S? cat C, dog D, cat hatesdog cat C, dog D, dog hatescat ( ) cat C, dog D, cat does not hatedog ( cat C, dog D, cat does not hatedog ) None of these Solution: The statement S can be expressed as S: cat C, dog D, cat hatesdog in predicate logic. Consequently S: cat C, dog D, cat does not hatedog, where
we have replaced by and negated the predicate. It is now clear that the third choice is logically equivalent to S, since it is a double negation thereof. d. In the following statements, P is the set of primes, N is the set of natural numbers, and Z is the set of integers. Which of these statements are true and which are false? True False p q implies q p Any implication implies its contrapositive, and vice versa. There is an integer b > 1 such that the number 100 b (expressed in base-b) is a prime. 100 b = b 2 for any base b, and b 2 cannot be a prime. The sum of 21 8 and 21 8 is 21 16. 21 8 + 21 8 = 2(2 8 + 1), which is even, but 21 16 = 2 16 + 1 is odd. n N, 2n is not in P. If n = 1, then 2n = 2 is a prime. m Z, n Z, n is a factor of (m 2 + 3m 18). n = m + 6 is always a factor of m 2 + 3m 18 = (m + 6)(m 3). n 2 1 is divisible by 4 whenever n is an odd integer. If n = 2t + 1, then n 2 1 = (n 1)(n + 1) = 4t(t + 1) 0 mod 4. The number 5 100 5 is divisible by 3. Since 5 2 mod 3 and 25 1 mod 3, we compute modulo 3 as follows: 5 100 5 = 25 50 5 1 50 2 1 mod 3. n 2 + 6n + 9 0 mod (n + 3), for all positive integers n. Evaluating n 2 + 6n + 9 modulo n + 3, we get ( 3) 2 + 6( 3) + 9 = 0. There exists integers a, b, such that gcd(a, b) + lcm(a, b) = a + b. If a = b, then gcd(a, b) = lcm(a, b) = a = b, so their sum is a + b. 2
Problem 2 (26 points) Consider the boolean function f(p, q, r) defined as follows: f(p, q, r) = (p q) ( q r ( q r) ) (1) where q is the negation of q, and denotes the implication connective. a. The truth table for f(p, q, r) can be obtained by combining the truth tables of the various statement forms from which f(p, q, r) is composed, as follows: p q r p q q r q r q r ( q r) f(p, q, r) 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 b. To express the function f(p, q, r) in disjunctive normal form (DNF), we can simply read off the three corresponding minterms from the truth table, and obtain: f(p, q, r) = pqr pq r pqr (2) c. To derive a simple expression for f(p, q, r), we can start from (1) while taking into account the truth table constructed in part (a). The truth table shows that q r ( q r) takes the value 1 if and only if q = r = 1. Thus we can immediately recognize that q r ( q r) q r. Consequently, f(p, q, r) can be simplified as follows: f(p, q, r) = (p q) ( q r ( q r) ) = (p q) (q r) = q (p r) where we have also used the distributive rule. Alternatively, we can start with the DNF in (2) and simplify using the algebraic rules as follows: f(p, q, r) = pqr pq r pqr = pqr ( pq ( r r) ) = pqr pq = q ( pr p ) = q ( ( p p) (r p) ) = q (p r) where we have used the fact that distributes over to get from pr p to ( p p) (r p). In both cases, the resulting circuit is given by: p r q f(p, q, r) 3
Problem 3 (14 points) The numbers x = 71 9 and y = 121 3 are expressed in the nonary (base-9) and the ternary (base-3) number systems, respectively. a. Convert x = 71 9 into the ternary number system. Answer: Since 9 = 3 2, we can easily compute as follows: 71 9 = 7 9 1 + 1 9 0 = (2 3 + 1) 3 2 + 1 3 0 = 2 3 3 + 1 3 2 + 0 3 1 + 1 3 0 = 2101 3 x = 2101 3 b. Compute the sum a = x + y using ternary arithmetic. Show all stages of your ternary computation and express its results in the ternary number system. Answer: Here is the ternary computation: 2 1 0 1 + 1 2 1 2 2 2 2 a = 2222 3 c. Convert the sum a = x + y from part (b) into the nonary number system. Answer: Since 9 = 3 2, we can easily compute as follows: 2222 3 = 2 3 3 + 2 3 2 + 2 3 1 + 2 3 0 = (2 3 + 2) 3 2 + (2 3 + 2) 3 0 = 8 9 1 + 8 9 0 Problem 4 (30 points) a. Let p be an odd prime. What is the remainder when p 2 is divided by 8? a = 88 9 Answer: Write p = 2t + 1 for t 1. Then p 2 = (2t + 1) 2 = 4t 2 + 4t + 1 = 4t(t + 1) + 1. Clearly, one of t or t + 1 is even (and the other is odd). Hence t(t + 1) is always divisible by 2, and therefore 4t(t + 1) is always divisible by 8. It follows that p 2 1 mod 8. p 2 1 mod 8 b. Let n 2 be an integer, and suppose that a and b are positive integers such that a n + 1 (mod 2n) and b n 1 (mod n) Find the unique integer c in the range 0 c n 1 which satisfies c 2a + 3b (mod n). Answer: In order to find c, it is important to realize that modulo n, we have a 1 and b 1. Thus c 2a + 3b 2 3 1 (mod n). The unique integer c in the range 0 c n 1 which satisfies this is c = n 1. c = n 1 4
c. Prove that for all positive integers n, we have gcd(9n + 4, 3n + 1) = 1. Answer: Dividing 3n + 1 into 9n + 4 with remainder, we get 9n + 4 = 3(3n + 1) + 1. Thus we can run the Euclidean algorithm to compute gcd(9n + 4, 3n + 1) as follows: gcd(9n + 4, 3n + 1) = gcd(3n + 1, 1) = 1 Extra-credit Problem 5 (30 points) Alice and Bob, who are good friends, receive an invitation to a high-stakes game show called Live Rich or Die. The invitation letter specifies the rules of the game as follows: The two of you will enter the game room, each wearing either a black hat or a white hat. You will be the only people in the room. You cannot see or know the color of your own hat, but you can see the color of the hat the other person is wearing. Within a minute of entering the game room, each of you should write on a card what the color of your own hat is. You will simultaneously hand these cards to the host when exiting the room. While in the room, you cannot communicate with each other in any way. If at least one of you guessed the color of his or her own hat correctly, you will each receive $1,000,000 congratulations! However, if you are both wrong or if you attempt to violate these rules in any way, you will both die. Alice and Bob certainly would not risk dying, even if there is a tiny chance of this happening. However, they would not mind getting rich either. They also took CSE20 last year, so they know a thing or two about propositional logic, statement variables, truth tables, and boolean functions. What would you advise Alice and Bob to do? Can they get rich on this show, without any chance of dying? If so, how exactly? Or, maybe, there is always a chance that they will die. If so, why? Answer: Alice and Bob can get rich on this show as follows. Alice should write on her card the color that she sees (of Bob s hat), while Bob should write on his card the opposite of the color that he sees. That is, if Alice is wearing white, Bob should write black, whereas if Alice is wearing black, Bob should write white. Here is why this always works. Let x denote the color of Alice s hat, and let y denote the color of Bob s hat. Since Alice sees Bob s hat, what she writes on her card can be a function of y. Similarly, what Bob writes on his card can be a function of x. Let f(y) and g(x) denote these two boolean functions. There are 2 21 = 4 different boolean functions of one variable, which means there are 4 4 = 16 cases to consider, as follows: f(y) = W, B, y, ȳ and g(x) = W, B, x, x (3) It is possible to arrive at the solution by writing out the truth tables for these 16 cases. However, here is a shortcut. The assertion that Alice guesses the color of her hat correctly can be expressed as the biconditional f(y) x. Similarly, g(x) y is true if and only if Bob guesses the color of his hat correctly. Since it will suffice for one of them to guess the color of his or her hat, what we want is that ( f(y) x ) ( g(x) y ) x f(y) x f(y) yg(x) ȳg(x) (4) is a tautology. Comparing (4) with the DNF of the tautology function, namely xy xy xȳ xȳ, we see that setting f(y) = y and g(x) = x works. Setting f(y) = ȳ and g(x) = x also works. No other choice in (3) will work, as you can verify by writing down the truth tables. 5