Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

Similar documents
1. Consider the conditional E = p q r. Use de Morgan s laws to write simplified versions of the following : The negation of E : 5 points

Mathematics for Computer Science Exercises for Week 10

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

All variables a, b, n, etc are integers unless otherwise stated. Each part of a problem is worth 5 points.

INTEGERS. In this section we aim to show the following: Goal. Every natural number can be written uniquely as a product of primes.

Section Summary. Proof by Cases Existence Proofs

MATH 55 - HOMEWORK 6 SOLUTIONS. 1. Section = 1 = (n + 1) 3 = 2. + (n + 1) 3. + (n + 1) 3 = n2 (n + 1) 2.

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

Fibonacci Nim and a Full Characterization of Winning Moves

MAT 243 Test 2 SOLUTIONS, FORM A

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

MATH10040: Numbers and Functions Homework 1: Solutions

The Fundamental Theorem of Arithmetic

MATH 215 Final. M4. For all a, b in Z, a b = b a.

NOTES ON SIMPLE NUMBER THEORY

Consider an infinite row of dominoes, labeled by 1, 2, 3,, where each domino is standing up. What should one do to knock over all dominoes?

Math 109 HW 9 Solutions

PUTNAM TRAINING MATHEMATICAL INDUCTION. Exercises

#G03 INTEGERS 10 (2010), MIN, A COMBINATORIAL GAME HAVING A CONNECTION WITH PRIME NUMBERS

12x + 18y = 50. 2x + v = 12. (x, v) = (6 + k, 2k), k Z.

arxiv: v2 [math.co] 9 Aug 2011

CSE 20 DISCRETE MATH WINTER

Homework #2 solutions Due: June 15, 2012

Predicate Logic - Undecidability

Fall 2017 Test II review problems

IS 709/809: Computational Methods for IS Research. Math Review: Algorithm Analysis

2.2 Inverses and GCDs

PRACTICE PROBLEMS: SET 1

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

3 - Induction and Recursion

Quiz 1, Mon CS 2050, Intro Discrete Math for Computer Science

Chapter 5.1: Induction

Discrete Mathematics & Mathematical Reasoning Induction

Name CMSC203 Fall2008 Exam 2 Solution Key Show All Work!!! Page (16 points) Circle T if the corresponding statement is True or F if it is False.

MADHAVA MATHEMATICS COMPETITION, December 2015 Solutions and Scheme of Marking

Lecture 7 Feb 4, 14. Sections 1.7 and 1.8 Some problems from Sec 1.8

NOTE: You have 2 hours, please plan your time. Problems are not ordered by difficulty.

then the hard copy will not be correct whenever your instructor modifies the assignments.

CS 360, Winter Morphology of Proof: An introduction to rigorous proof techniques

Simultaneous Linear, and Non-linear Congruences

Final Exam Review. 2. Let A = {, { }}. What is the cardinality of A? Is

Olympiad Number Theory Through Challenging Problems

Discrete Mathematics & Mathematical Reasoning Induction

Single Pile (Move Size) Dynamic Blocking Nim

CS280, Spring 2004: Prelim Solutions

Greatest Common Divisor MATH Greatest Common Divisor. Benjamin V.C. Collins, James A. Swenson MATH 2730

Homework 1. 10(b) A useful denial of the statement We will win the first game or the second one is We will lose the first two games.

Discrete Math, Second Problem Set (June 24)

Number Theory Solutions Packet

Problem Set #3 Solutions

Math 283 Spring 2013 Presentation Problems 4 Solutions

1 Overview and revision

Proof 1: Using only ch. 6 results. Since gcd(a, b) = 1, we have

Chapter 2 Section 2.1: Proofs Proof Techniques. CS 130 Discrete Structures

2301 Assignment 1 Due Friday 19th March, 2 pm

Solving Diophantine Equations With Unique Factorization

ALGEBRA. 1. Some elementary number theory 1.1. Primes and divisibility. We denote the collection of integers

MAT 300 RECITATIONS WEEK 7 SOLUTIONS. Exercise #1. Use induction to prove that for every natural number n 4, n! > 2 n. 4! = 24 > 16 = 2 4 = 2 n

arxiv: v1 [math.co] 27 Aug 2015

Advanced Analysis of Algorithms - Midterm (Solutions)

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

Strong Induction (Second Principle) Example: There are two piles of cards, players take turn: each turn: one player removes any number of cards from

Direct Proof and Proof by Contrapositive

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

MATH 501 Discrete Mathematics. Lecture 6: Number theory. German University Cairo, Department of Media Engineering and Technology.

5: The Integers (An introduction to Number Theory)

Slow k-nim. Vladimir Gurvich a

CS Asymptotic Notations for Algorithm Analysis

Math 3000 Section 003 Intro to Abstract Math Homework 6

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

Solutions, 2004 NCS/MAA TEAM COMPETITION

The following techniques for methods of proofs are discussed in our text: - Vacuous proof - Trivial proof

Discrete Mathematics with Applications MATH236

Linear Congruences. The equation ax = b for a, b R is uniquely solvable if a 0: x = b/a. Want to extend to the linear congruence:

International Mathematics TOURNAMENT OF THE TOWNS

Solutions Math 308 Homework 9 11/20/2018. Throughout, let a, b, and c be non-zero integers.

Problems and Solutions

Notes on induction proofs and recursive definitions

1. multiplication is commutative and associative;

An integer p is prime if p > 1 and p has exactly two positive divisors, 1 and p.

Sums of Squares. Bianca Homberg and Minna Liu

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

Announcements. Read Section 2.1 (Sets), 2.2 (Set Operations) and 5.1 (Mathematical Induction) Existence Proofs. Non-constructive

2k n. k=0. 3x 2 7 (mod 11) 5 4x 1 (mod 9) 2 r r +1 = r (2 r )

M381 Number Theory 2004 Page 1

Ma/CS 6a Class 2: Congruences

Math 3012 Applied Combinatorics Lecture 4

WORKSHEET MATH 215, FALL 15, WHYTE. We begin our course with the natural numbers:

CS Asymptotic Notations for Algorithm Analysis Outline

MATH 420 FINAL EXAM J. Beachy, 5/7/97

3.2 Solving linear congruences. v3

Top Down Design. Gunnar Gotshalks 03-1

MAS114: Solutions to Exercises

The group (Z/nZ) February 17, In these notes we figure out the structure of the unit group (Z/nZ) where n > 1 is an integer.

(e) Commutativity: a b = b a. (f) Distributivity of times over plus: a (b + c) = a b + a c and (b + c) a = b a + c a.

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 1

CmSc 250 Intro to Algorithms. Mathematical Review. 1. Basic Algebra. (a + b) 2 = a 2 + 2ab + b 2 (a - b) 2 = a 2-2ab + b 2 a 2 - b 2 = (a + b)(a - b)

Cryptography and Security Midterm Exam

Problem Set 5 Solutions

Transcription:

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2 CS 30, Fall 2018 by Professor Prasad Jayanti Problems 1. Let g(0) = 2, g(1) = 1, and g(n) = 2g(n 1) + g(n 2) whenever n 2. What is g(4)? g(2) = 2g(1) + g(0) = 4. g(3) = 2g(2) + g(1) = 9. g(4) = 2g(3) + g(2) = 22. 2. Prove by induction that n 2 1 is divisible by 8 whenever n is an odd positive integer. (It is possible to prove this statement in other ways, but I require your proof be by induction.) Let P (n) state If n is odd, then n 2 1 is divisible by 8. Claim: n 1, P (n) Proof: By strong induction. Base case: P (1) is true since 1 2 1 = 0 is divisible by 8. Induction step: Let k 1 be arbitrary and assume that P (1), P (2),..., P (k) are true. If k + 1 is even, then P (k + 1) is trivially true. Suppose that k + 1 is odd. Since k 1 and k + 1 is odd, it follows that k + 1 3. So, k 1 1 and k 1 is is odd. By the induction hypothesis P (k 1) is true and, since k 1 is odd, it follows that (k 1) 2 1 is divisible by 8. Further, since k is even and positive, k is divisible by 2; so, 4k is divisible by 8. It follows from the last two sentences that ((k 1) 2 1) + 4k is divisible by 8. Since (k + 1) 2 1 = ((k 1) 2 1) + 4k, it follows that (k + 1) 2 1 is visible by 8. Hence, we have P (k + 1). The claim then follows from the principle of strong induction. 3. Prove by induction that if n is a positive integer, then 133 divides 11 n+1 + 12 2n 1. Let P (n) state 11 n+1 + 12 2n 1 is divisible by 133. Claim: n 1, P (n) Proof: By weak induction. Base case: P (1) is true since 11 1+1 + 12 2 1 1 = 133 is divisible by 133. Induction step: Let k 1 be arbitrary and assume that P (k) is true, i.e., 11 k+1 +12 2k 1 is divisible by 133. To show that P (k + 1) is true, we now show that 11 (k+1)+1 + 12 2(k+1) 1 is divisible by 133. We have 11 (k+1)+1 + 12 2(k+1) 1 = 11 k+2 + 12 2k+1 = 11 11 k+1 + 12 2 12 2k 1 = 11 (11 k+1 + 12 2k 1 ) + 133 12 2k 1. In the final expression, since the first term is divisible by 133 (by the induction hypothesis), it follows that the whole expression is divisible by 133. Hence, we have P (k + 1). The claim then follows from the principle of weak induction. 4. Consider this variation of the game of Nim. The game begins with n matches, where n > 0. Two players take turns, removing matches, one, two, or three at a time. The player removing the last 1

match loses. Prove by induction that if each player plays the best strategy possible, the first player wins if n mod 4 is 0, 2, or 3, and the second player wins if n mod 4 is 1. Proof: Let P (n) be a predicate that states that if there are n matches, the player with the first turn has a winning strategy if n mod 4 is 0, 2, or 3, and the player whose turn is second has a winning strategy if n mod 4 is 1. Claim: n 1, P (n) Proof: We prove the claim by strong induction. Base Cases: P (1) is true because, if the initial pile has only one match, the first player is forced to remove that match, which causes him to lose the game. P (2) and P (3) are true because, if the initial pile has 2 or 3 matches, the first player can remove 1 or 2 matches, respectively, thereby forcing the second player to remove the lone remaining match. Induction Step: Assume that k 3 and P (1), P (2),..., P (k) are true (this assumption is the Induction Hypothesis). To complete the induction step, we must show P (k + 1). Assume the pile has k + 1 matches. Since k 3, k + 1 is at least 4. We consider two cases: either (k + 1) mod 4 is 0, 2, or 3, or (k + 1) mod 4 is 1. If (k + 1) mod 4 is 0, 2, or 3, then the first player A can remove 3, 1, or 2 matches, respectively, thereby leaving l matches such that l mod 4 is 1. Now it is the other player B s turn. Since 1 k 2 l k, P (l) holds (by induction hypothesis). Since it is now B s turn and l mod 4 is 1, P (l) implies that A has a winning strategy. Hence, we have P (k + 1). If (k + 1) mod 4 is 1, then regardless of whether the first player A removes 1, 2, or 3 matches, the number l of matches left satisfies 1 k 2 l k and l mod 4 {0, 2, 3}. Therefore, by induction hypothesis P (l) holds. Since it is B s turn and l mod 4 {0, 2, 3}, P (l) implies that B has a winning strategy. Hence, we have P (k + 1). This completes the induction step. The claim then follows from the principle of strong mathematical induction. 5. Define a set S {0, 1} by the following rules: 0 S. If x S then 0x11 S. Describe the above set S in the set builder notation. (In your answer you can use the notation a m b n for a string of m a s followed by n b s.) {0 n 01 2n n 0} = {0 n+1 1 2n n 0} 6. Give an inductive definition for the set D of all integers that are divisible by 5 or 3. For example, 10, -24, 15 are in D, but 8 is not in D. In your inductive definition, you may use + and operations, but not (i.e., the multiplication) operation. 2

0, 3, 5 D. If n D and n + 3 D, then n 3 D and n + 6 D. If n D and n + 5 D, then n 5 D and n + 10 D. 7. Define a set S {0, 1} by the following rules: 0 S. If x S, then x1 and 0x1 are in S. You will now prove that S = T, where T = {0 m 1 n n + 1 m 1}, in the following two steps: (a) Prove by structural induction that every element of S is in T (i.e., S T ). Claim: x S, x T Proof: We prove the claim by structural induction. Base Case: 0 T by the definition of T. Hence, we have the base case. Induction Step: Let x be an arbitrary element of S and assume that x T. Then, by definition of T, we have x = 0 m 1 n, where n + 1 m 1. Then: x1 is 0 m 1 n+1, where (n + 1) + 1 > m 1. So, x1 T. 0x1 is 0 m+1 1 n+1, where (n + 1) + 1 m + 1 > 1. So, 0x1 T. Hence, we have the induction step. The claim then follows from the principle of structural induction. (b) Prove by strong induction that every element of T is in S (i.e., T S). Let P be a predicate on Z such that P (z) if 0 m 1 n T and m + n = z, then 0 m 1 n S. Claim: z 1, P (z) Proof: We prove the claim by strong induction. Base Case: Suppose x = 0 m 1 n T and m + n = 1. Since m 1, it follows that m = 1 and n = 0. Thus, 0 m 1 n = 0, which is in S. Hence, we have the base case. Induction Step: Let z 1 be arbitrary and assume that P (1), P (2),..., P (z) are true. To show P (z + 1), we need to argue that if 0 m 1 n T and m + n = z + 1, then 0 m 1 n S. Assume that 0 m 1 n T and m + n = z + 1. Since 0 m 1 n T, we have n + 1 m 1. There are two cases: either n + 1 > m 1 or n + 1 = m 1. Suppose that n + 1 > m 1. It follows that n 1; so, n 1 0. Then, 0 m 1 n 1 T since (n 1) + 1 = n m 1. Further, since m + (n 1) = z and P (z) is true, we have 0 m 1 n 1 S. Then, by the first recursive rule in the definition of S, we have 0 m 1 n 1 1 = 0 m 1 n S. Suppose that n + 1 = m 1. Substituting m = n + 1 in m + n = z + 1 2, we get 2n + 1 2. Since n is an integer, it follows that n 1. Since m = n + 1, we have m 2. So, m 1 0 and n 1 0. Further, (n 1) + 1 = m 1 1. So, 0 m 1 1 n 1 T. Moreover, (m 1) + (n 1) = z 2 1 (since z = m + n and m 2, n 1). Therefore, by induction hypothesis, 0 m 1 1 n 1 S. Then, by the second recursive rule in the definition of S, we have 0 0 m 1 1 n 1 1 = 0 m 1 n S. Hence, we have the induction step. The claim then follows from the principle of strong induction. 3

8. Read the following recursive method carefully and understand what it is accomplishing. mymethod(a: array of integers; i, j: integer) { 1. if i = j return i 2. m (i + j)/2 3. p mymethod(a, i, m) 4. q mymethod(a, m + 1, j) 5. if a[p] > a[q] return p else return q } State a predicate that helps you make as strong a claim as possible about what this recursive method accomplishes, and then prove your claim by induction. Let P (n) be the predicate that states If a[i j] = n, then mymethod(a, i, j) terminates and returns x such that i x j, a[x] is the maximum value in a[i... j], and a[x] is strictly greater than each element of a[x + 1... j]. Claim: n 0, P (n) Proof: By strong induction. Base case: P (0) states that mymethod(a, i, i) terminates and returns i, which is true by Lines (1) and (2). Induction step: Let k 0 be arbitrary, and assume that P (0), P (1),..., P (k) are true. We show below that P (k + 1) is true. Consider a call to mymethod(a, i, j) such that a[i j] = k + 1. Since k 0, it follows that i < j; so, control goes to Line (3). Since i < j and m = (i + j)/2, it follows that i m < j. Therefore, 0 m i k and 0 j (m + 1) k. From the induction hypothesis, it follows that the recursive call at Line (4) terminates and sets p to a value between i and m such that a[p] is the maximum of a[i... m] and a[p] is strictly greater than each element of a[p + 1... m]. Similarly, it follows from the induction hypothesis that the recursive call at Line (5) terminates and sets q to a value between m + 1 and j such that a[q] is the maximum of a[m + 1... j] and a[q] is strictly greater than each element of a[q + 1... j]. Therefore, if a[p] > a[q] holds at Line (6), it follows that p is a value between i and j, a[p] is the maximum of a[i... j], and a[p] is strictly greater than each element of a[p + 1... j]; hence, the return of p at Line (6) ensures P (k + 1). On the other hand, if a[p] a[q] holds at Line (6), it follows that q is a value between i and j, a[q] is the maximum of a[i... j], and a[q] is strictly greater than each element of a[q + 1... j]; hence, the return of q at Line (6) ensures P (k + 1). Hence, we have the induction step. The claim then follows from the principle of strong induction. 9. Read the following recursive method carefully and understand what it is accomplishing. mymethod(a, b: array of integers; i, j: integer) { 1. c true 2. if i < j 3. c mymethod(a, b, i + 1, j) 4. return (c (a[i] = b[i])) } 4

State a predicate that helps you make as strong a claim as possible about what this recursive method accomplishes, and then prove your claim by induction. Let P (n) be the predicate that states If n = a[i j], then mymethod(a, i, j) terminates and it returns true if and only if a[i j] = b[i j] (i.e., i p j, a[p] = b[p]). Claim: n 1, P (n) Proof: By weak induction. Base case: P (1) states that mymethod(a, b, i, i) terminates and it returns true if and only if a[i] = b[i], which is true because, when executing mymethod(a, b, i, i), the program performs Line 1, skips Lines 2 and 3, and returns true (a[i] = b[i]). Induction step: Let k 1 be arbitrary, and assume that P (1), P (2),..., P (k) are true. We show below that P (k + 1) is true. Consider a call to mymethod(a, i, j) such that a[i j] = k + 1. Since k 1, it follows that i < j; so, control goes to Line 3. Since a[i + 1 j] = k, it follows from the induction hypothesis that the recursive call at Line 3 terminates and sets c to true if and only if a[i + 1 j] = b[i + 1 j]. Therefore, the method terminates after Line 4, and the value c (a[i] = b[i]) returned at Line 4 is true if and only if a[i j] = b[i j]. Hence, we have P (k + 1). The claim then follows from the principle of weak induction. 10. Use Euclid s algorithm to determine whether 749 and 1605 are relative primes. Show your work. Euclid(749, 1605) calls Euclid(107, 749), which returns 107. 11. It turns out there are many integers x such that 70x mod 81 = 1. Use ExtendedEuclid algorithm to find a value for x. If 70x mod 81 = 1, then there must be an integer y such that 70x + 81y = 1. We can find x and y satisfying this equation by calling ExtendedEuclid(70, 81). EE(70, 81) calls EE(11, 70), which calls EE(4, 11), which calls EE(3, 4), which calls EE(1, 3), which returns (1, 0, 1). Then, EE(3, 4) returns ( 1, 1, 1). Then, EE(4, 11) returns (3, 1, 1). Then, EE(11, 70) returns ( 19, 3, 1). Then, EE(70, 81) returns (22, 19, 1). Therefore, x = 22. 12. Prove that if the gcd of two positive integers n and m is 10, then (4 n 4 m) is false. Proof: We prove by contradiction. Assume that gcd(n, m) = 10, and (4 n 4 m) is true. Since gcd(n, m) = 10, we know from the property of ExtendedEuclid that there are integers x and y such that nx + my = 10. Since 4 n and 4 m, it follows that 4 (nx + my); however, 4 does not divide 10, which contradicts that nx + my = 10. 13. Here is a variation of Euclid s algorithm. 5

myeuclid(n, m: integer) { 1. if n divides m then return n 2. if (m mod n) divides n then return (m mod n) 3. return myeuclid(n mod (m mod n), m mod n) } Is it true that, for all m n 1, myeuclid(n, m) terminates and returns gcd(n, m)? If it is true, prove rigorously. Otherwise give a counterexample. The statement is true, as implied by the following claim, where P (n) states that If m n > 0, then myeuclid(n, m) terminates and returns gcd(n, m). In the proof of the claim below, we use the following fact that we proved in class: If m n 1 and n does not divide m, then gcd(n, m) = gcd(m mod n, n). Claim: n 1, P (n). Proof: By strong induction. Consider the execution of Euclid(1, m), where m 1. Since 1 m, the method terminates at Line 1 and returns 1. Since gcd(1, m) = 1, we have P (1). Induction step: Let k 1 be arbitrary and asume that P (1), P (2),..., P (k) are true. Consider the execution of ExtendedEuclid(n, m), where m n = k + 1. There are three cases to consider: (i) n divides m, (ii) n does not divide m, but m mod n divides n, or (iii) n does not divide m, and m mod n does not divide n If n divides m, gcd(n, m) = n and the method terminates at Line 1 returning n. Hence, we have P (k + 1). If n does not divide m, it follows from the above mentioned fact from class that gcd(n, m) = gcd(m mod n, n). Further, if m mod n divides n, then gcd(m mod n, n) = m mod n, which is what the method returns at Line 2. Hence, we have P (k + 1). If n does not divide m, then m mod n 0 and gcd(n, m) = gcd(m mod n, n). If m mod n does not divide n, then 0 < (n mod (m mod n)) < (m mod n) and, by the above mentioned fact from class, gcd(m mod n, n) = gcd(n mod (m mod n), m mod n). Further, by the induction hypothesis P (n mod (m mod n)) is true. So, the recursive call terminates and returns gcd(n mod (m mod n), m mod n). Since this value is gcd(n, m), we have P (k + 1). Hence, we have the induction step. The claim then follows from the principle of weak induction. 6