# Second Midterm Exam

### Directions:

• Answer the problems on the exam pages.
• There are five problems on pages 2-6, for 100 total points. Probable scale is A=90, C=60.
• If you need extra space use the back of a page.
• No books, notes, calculators, or collaboration.

```  Q1: 25 points
Q2: 15 points
Q3: 20 points
Q4: 15 points
Q5: 25 points
Total: 100 points
```

• 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
• (a,5) Give a regular expression denoting Z (no justification needed).
• (b,5) Prove by induction on the definition that every string in Z has even length.
• (c,5) Prove by induction on the definition that no string in Z ends with ``aa".
• (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.)

• 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.

• 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).)
• (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.)

• 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.)

• 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. There are no other edges in the graph. 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.)
• (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).)