# Second Midterm Exam Solutions

#### 1 April 2005

Question text is in black, solutions in blue.

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

T(0) = 0, and T(n+1) = T(n) + (n+1)(n+2)/2 since the "k" in the triangle of cannonballs added is n+1. Let P(n) be the statement "T(n) = n(n+1)(n+2)/6".

Base Case: P(0) says that T(0) = 0(0+1)(0+2)/6 = 0, which is true according to the definition.

Inductive step: Assume that P(n) is true. We are given that T(n+1) = T(n) + (n+1)(n+2)/2, so the IH tells us that T(n+1) = n(n+1)(n+2)/6 + (n+1)(n+2)/2. By arithmetic, this is equal to (n+1)(n+2)[n/6 + 1/2] = (n+1)(n+2)[(n+3)/6] which is what T(n+1) is supposed to be according to P(n+1). We have completed the inductive step and thus the proof.

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

By successive substitution into the definition, Z(13) = 2 times Z(6) = 4 times Z(3) = 8 times Z(1) = 8.

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

Let P(n) be the statement "`zcode` terminates on input n and returns Z(n)".

First Base Case: If n=0, Z(0) is defined to be 0, and the method returns 0 with no recursion.

Second Base Case: If n=1, Z(1) is defined to be 1, and the method returns 1 with no recursion.

Strong Inductive Step: Let n ≥ 1 and assume P(i) for all i with i ≤ n. P(n+1) says that the method terminates on input n+1, with output Z(n+1). Z(n+1) is defined to be 2 times Z((n+1)/2), since n+1 > 1. On input n+1, the code recursively calls `zcode((n+1)/2)`. Since (n+1)/2 ≤ n (see the fall 2004 exam solutions for a proof), the strong IH tells us that P((n+1)/2) is true and thus that the recursive call terminates and returns a value of Z((n+1)/2). The original method takes this value, doubles it, and returns it, thus returning the value 2 times Z((n+1)/2) or Z(n+1). We have proved P(n+1), so we have completed the inductive step and thus the proof.

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

Let Q(n) be the statement "Z(n) is a power of two". We prove Q(n) by strong induction on positive naturals:

Base Case: For n=1, Z(n) = 1 by definition of Z, and 1 is defined to be a power of two, so P(1) is true.

Strong Inductive Case: Assume that P(i) is true for all i with i ≤ n. Z(n+1) is defined to be 2 times Z((n+1)/2). Since (n+1)/2 ≤ n, P((n+1)/2) is true as port of the strong IH, so we know that Z((n+1)/2) is a power of two. Since Z(n+1) is twice a power of two, by the definition it is also a power of two and P(n+1) is true. We have completed the strong induction and thus the proof.

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

22 = 2 times 11, 27 = 3 times 3 times 3, greatest common divisor = 1 since no prime occurs in both factorizations, least common multiple = 22 times 27 = 594 since it is (22 times 27) divides by the gcd.

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

Inverse algorithm:

• 27 = 1(27) + 0(22)
• 22 = 0(27) + 1(22)
• 5 = 1(27) - 1(22)
• 2 = -4(27) + 5(22)
• 1 = 9(27) - 11(22), check 1 = 243 - 242

So we let a be 9 and let b be -11.

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

Since 9 times 27 is shown above to be 1 plus a multiple of 22, we have that 9(27) ≡ 1 (mod 22). Thus 9 is an inverse of 27, modulo 22. Any integer of the form 9 = 22k, where k is an integer, is also an inverse, so the naturals 9, 31, 53, 74, ..., are all correct answers to this question.

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

By reasoning similar to that in (d), we see that -11 is an inverse of 22, modulo 27. Thus -11(22) ≡ 1 (mod 27), and clearly -11(22) ≡ 0 (mod 22). But our answer must be a natural. We can add 27(22) = 594 to the answer and still have it satisfy both congruences (since the Chinese Remainder Theorem says that they are equivalent to a single congruence mod 594). This gives us (-11+27)(22) = 16(22) = 352. To check, 352 = 13(27) + 1.

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

The standard way to solve this problem from the proof of the Chinese Remainder Theorem is as follows: (20)(9)(27) + (15)(-11)(22) is congruent to (20)(9)(27) ≡ 20 (mod 22) because 9 is the inverse of 27 modulo 22, and the same number is congruent to (15)(-11)(22) ≡ 15 (mod 27), because -11 is the inverse of 22 modulo 27. This gives 4860 - 3630 = 1230.

In this particular case it is easier to look for small solutions. The naturals that are congruent to 15 modulo 27 are 15, 42, 69, 96, 123,..., and the naturals that are congruent to 20 modulo 22 are 20, 42, 64, 86, 108, 130,..., so if we notice that 42 (the answer to life, the universe, and everything) is on both lists we have our solution.

Of course both answers can be correct since they are congruent modulo 594.

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

For the base case, any string in Σ*b is equal to wb for some string b, so it is in X by Rule 2. For the inductive step, let w be an arbitrary string and assume that w is in X. Then by Rule 3, wa is in Y. By Rule 4, waa is in X. We have proved P(w) → P(waa) and completed the induction.

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

Base Case: By Rule 1, λ∈ X, so λ∈ X∪Y and Q(λ) is true.

First Inductive Step: Assume w ∈ X∪Y. If w ∈ X, then by Rule 3, wa ∈ Y so wa ∈ X∪Y and Q(wa) is true. If w ∈ Y, then by Rule 4, wa ∈ X so wa ∈ X∪Y and Q(wa) is true. Since the IH says that either w∈X or w∈Y, the IH implies Q(wa).

Second Inductive Step: For any string w, by Rule 2, wb ∈ X and thus wa ∈ X∪Y and Q(wb) is true. So the implication Q(w) → Q(wb) is true by the rule of Trivial Proof.

Since the base case and both inductive steps are proved, by string induction Q(w) is proved for any string w in Σ*.