# Second Midterm Exam Solutions

#### 29 October 2004

• Question 1 (20): Let Z be the language of strings over the alphabet {a,b} defined by the following rules:
• The string ba is in Z
• If any string u is in Z, then so are the strings uab, uba, and ubb
• No other strings are in Z \end{itemize} Answer the following questions about Z: \begin{itemize}
• (a,5) Give a regular expression denoting Z (no justification needed).

ba(ab+ba+bb)*

• (b,5) Prove by induction on the definition that every string in Z has even length.

Base case: ba has length 2, which is even.

Induction case: Let u be a string and assume that u has some even length 2k. Then uab, uba, and ubb each have length 2k+2, which is even.

Since the property holds for the base case and is preserved by the inductive steo it holds for all strings in Z.

• (c,5) Prove by induction on the definition that no string in Z ends with aa".

Base: ba does not end in aa.

Induction: Assuming u does not end in aa, neither do uab, uba, or ubb. (Note that no use is made of the IH here.)

As in (a), this suffices to prove it for all strings in Z.

• (d,10) Prove by induction on the definition that no string in Z has three a's in a row. (Hint: Use (c), even if you were unable to prove it.)

Base: ba does not have three a's in a row.

Induction: Let u be an arbitrary string in Z. By (c), u does not end in aa, and by the IH, u does not have three a's in a row. Now look at uab, uba, and ubb. The latter two certainly could only have three a's in a row if u did, and it doesn't. The string uab could have three a's in a row only if either u did or if u ended in aa, neither of which is true.

We have shown that (u∈Z) and P(u) together imply P(uab), P(uba), and P(ubb), which is enough to complete the induction and thus the proof.

• Question 2 (15): Prove the statement ∀a:∀b:(ab>0)→[∃c:(c≡3 (mod a))∧ (c≡3 (mod b))]

where the type of the variables a, b, and c is natural. Recall that x≡y (mod z) is defined to be true if and only if z divides the integer x-y.

Let a and b be arbitrary. Assume that ab > 0. It follows that a > 0 and b > 0, because if either of a and b were 0 then so would be ab. Let c = ab+3. Then the difference between c and 3 is ab, which is divisible by both a and b, so c is congruent to 3 both mod a and mod b. Since we found a suitable c, we have proved the ∃c statement by the Rule of Existence. Since a and b were arbitrary, we have proved the whole statement.

Some students let c=3 rather than c=ab+3, which is a perfectly good answer because both a and b divide 0.

• Question 3 (20): Recall that the Fibonacci numbers are defined by the rules F(0) = 0, F(1) = 1, and for all positive naturals n, F(n+1) = F(n) + F(n-1).
• (a,10) Prove by induction that for all positive naturals n, F(n+2) > F(n+1) and F(n+1) ≥ F(n). (It follows that for all n with n≥ 2, F(n+1) % F(n) = F(n-1) where "%" is the Java integer modulus operator -- this fact is useful in part (b).)

Let P(n) be the statment that F(n+2) > F(n+1) and F(n+1) ≥ F(n).

Base: Since we are proving this for all positive n, our base case is P(1), which is a good thing since P(0) is false. P(1) is true because F(3) = 2 > F(2) = 1 ≥ F(1) = 1.

Induction: Assume P(n), so that F(n+2) > F(n+1) and F(n+1) ≥ F(n). We want to prove that F(n+3) > F(n+2) and that F(n+2) ≥ F(n+1). The first statement is true because F(n+3) = F(n+2) + F(n+1) > F(n+1) = F(n+1) ≥ F(n+1) + F(n) = F(n+2). The second statement follows immediately from the assumption \$F(n+2) > F(n+1). This proves P(n+1), completing the induction and the proof.

• (b,10) Prove by induction for all naturals n that on the inputs F(n+1) and F(n), the Euclidean Algorithm stops after at most n steps. (It is defined to stop as soon as one of its two numbers is zero.)

Base: P(0) says that on input F(1) = 1 and F(0) = 0, the EA stops in zero steps. This is true because the second argument is already 0.

Induction: Assume that the EA stops in at most n steps on F(n+1) and F(n+2). Consider starting the EA on inputs F(n+2) and F(n+1). By the observation in (a), F(n+2) % F(n+1) = F(n) unless n=0 or n=1, so except in these cases, after its first step the EA continues on inputs F(n+1) and F(n). By the IH this takes at most n more steps, making at most n+1 in all. If n=0, then the EA on input F(2) = 1 and F(1) = 1 takes one step, which is fine since 1 = n+1. If n=1, the EA on input F(3) = 2 and F(2) = 1 also takes one step, and since 1 ≤ n+1 this is fine. This completes the induction and thus the proof.

• Question 4 (15): Define the predicate PD(n) to mean "{Either n ≤ 1 or there exist naturals b and c such that n=bc and b is prime." Prove ∀n:PD(n) by strong induction. (Hint: In the inductive step, divide into cases based on whether n+1 is prime. Recall that if a number is not prime, the definition of primality tells you that certain other numbers exist.)

We use strong induction on n with "PD(n)" as our predicate.

Base: The statements PD(0) and PD(1) are trivially true by joining.

Induction: Assume PD(i) for all i with i ≤ n, and we will prove P(n+1). If n=0, then PD(n+1) = PD(1) is trivially true by joining. If n+1 is prime, let b = n+1 and c = 1, and we have proved PD(n+1). Otherwise n+1 is composite, so that it equals de for some naturals d and e, each greater than 1 and less than n+1. The strong IH tells us that PD(d) is true, so there exist numbers f and g such that f is prime and fg = d. But then fge = n+1, and by taking b to be f and c to be ge, we have that b is prime and bc=n+1 and we have proved PD(n+1). This completes the strong induction and thus the proof.

• Question 5 (25): Define an undirected graph G as follows. There are 1000 vertices, named by the numbers from 0 through 999. Every vertex n with n ≤ 993 has an edge to vertex n+6, and every vertex n with n ≤ 990 has an edge to vertex n+9. Recall that the predicate Path(u,v) on vertices is defined by the rules Path(u,u) and Path(u,v) ∧ Edge(v,w) → Path(u,w).
• (a,10) Prove for any vertices u and v that if Path(u,v), then u ≡ v (mod 3). (Use induction on the definition of paths.)

Let u be arbitrary and use induction on v such that Path(u,v) is true.

Base: u = v, and then u ≡ v (mod 3) is true because 3 divides 0.

Induction: Assume that Path(u,w), that u ≡ w (mod 3), and that Edge(w,v). Then v must be equal to w-9, w-6, w+6, or w+9. All these numbers are congruent to w modulo 3, so by the IH they are also congruent to u modulo 3. This completes the induction on paths and thus the proof.

• (b,15) Prove for any vertices u and v that if u ≡ v (mod 3), then Path(u,v). (Hint: Let u be an arbitrary vertex and prove by strong induction on k that if v = u + 3k or v = u - 3k, and v is the number of a vertex, then Path(u,v).)

Following the hint, let u be arbitrary, and let P(k) be "if v = u+3k or v = u-3k and v is the number of a vertex, then Path(u,v)". We will prove P(k) for all naturals by strong induction on k.

Base: k=0, so v = u and Path(u,v) is true by the base case of the Path definition.

Induction: Assume P(i) for all i with i ≤ k, and we will prove P(k+1). Let v be an arbitrary vertex. Assume v = u + 3(k+1) = u + k + 3. If u and k are both 0, then v = 3 and Path(0,3) is true because there is an edge from 0 to 9 and an edge from 3 to 9. Otherwise v - 6 = u + k - 3 is a vertex number. Then the IH tells us P(k-1) is true and thus that Path(u,v-6) and thus P(u,v) are true, unless k = 0. But if k = 0 then v = u+3 and we can connect u to v by a two-step path either from u to u+9 to u+3 or from u to u-6 to u+3. (Since the length of the graph is more than 15, at least one of u+9 and u-6 is the number of a vertex.) This completes the case of v = u + 3(k+1).

Now assume that v = u - 3(k+1). If k=0, connect u to u-3 by a two-step path either through u-9 or through u+6, as at least one of these vertices exists. Otherwise connect v to v+6 = u - 3(k-1), and then connect v+6 to u using the strong IH which includes P(k-1).