# Second Midterm Exam

### Directions:

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

```  Q1: 15 points
Q2: 25 points
Q3: 30 points plus 5 extra credit
Q4: 30 points
Total: 100 points plus 5 extra credit
```

• Question 1 (15): Define the function T(n) to be the number of cannonballs in a tetrahedral stack of height n. Tetrahedral stacks are defined recursively as follows:

• The stack of height 0 has no cannonballs.

• For any natural n, the stack of height n+1 is made by placing a stack of height n on top of a triangle of cannonballs with n+1 cannonballs on a side. (Recall that a triangle of side k has Σi=1k i, the sum for i from 1 to k of i, or k(k+1)/2 cannonballs in it.)

Write an equation for T(n+1) in terms of T(n), and use this equation to prove by (ordinary) induction that for any natural n, T(n) = n(n+1)(n+2)/6.

• Question 2 (25): The function Z from naturals to naturals is defined as follows:

• Z(0) = 0.

• Z(1) = 1.

• If n > 1, then Z(n) = 2 times Z(n/2), where the "/" is Java integer division with no remainder.

The following pseudo-Java method is intended to compute Z(n):

``````
public natural zcode (natural n)
{// Returns Z(n) as defined above
if (n <= 1) return n;
return 2 * zcode (n/2);}
``````

This question have three parts, all of which refer to the function Z:

• (a,5) Find the value of Z(13), from either the code or the definition. Show your reasoning.

• (b,10) Prove that the method `zcode` does in fact compute Z(n) as defined. (Hint: Use strong induction on n, with two base cases.)

• (c,10) The naturals that are powers of two are defined as follows:
• 1 is a power of two.
• If x is a power of two, then so is 2x.
• Nothing else is a power of two.

Prove by (strong) induction on all positive naturals n that Z(n) is a power of two, where Z is the function defined above. (Make sure that you use the appropriate base case for positive naturals.)

• Question 3 (30+5): This question deals with number-theoretic properties of the naturals 22 and 27.

• (a,5) Give the prime factorizations of 22 and 27, their greatest common divisor, and their least common multiple. (You may express the last as a product without evaluating the product.) No justification is needed if your answer is right.

• (b,10) Find integers a and b such that (a times 27) + (b times 22) = 1. Remember that integers may be positive, zero, or negative.

• (c,5) Find a natural that is the inverse of 27, modulo 22.

• (d,10) Find a natural c such that c ≡ 1 (mod 27) and c ≡ 0 (mod 22).

• (e, 5 extra credit) Find a natural d such that d ≡ 15 (mod 27) and d ≡ 20 (mod 22). (Your answer may be expressed as a sum or product without being calculated out fully.)

• Question 4 (30): This question deals with two languages X and Y over the alphabet Σ = {a, b}, defined by the following rules:

1. The empty string λ is in X.

2. If w is any string at all, then wb is in X.

3. If w is any string in X, then wa is in Y.

4. If w is any string in Y, then wa is in X.

• (a,15) Prove that every string in the regular language Σ*b(aa)* is in X. (Hint: Use induction on the definition of the star language. Let P(w) be the predicate "w ∈ X". Your base case is to prove P(w) for all w in Σ*b. Your inductive step is to prove ∀ w: P(w) → P(waa).)

• (b,15) Prove that X ∪ Y = Σ* by proving that every possible string is either in X or in Y, or both. (Hint: Use string induction where Q(w) is the predicate "w ∈ X ∪ Y". Prove Q(λ), ∀ w: Q(w) → Q(wa), and ∀ w: Q(w) → Q(wb).