# Solutions to Second Midterm Exam

#### 3 April 2006

Questions are in black, solutions are in blue.

• Question 1 (20): For any natural n, define A(n) to be the alternating sum of the first n odd numbers, that is, the sum for i from 1 to n of (-1)i(2i-1).

For example, A(0) = 0, A(1) = -1, A(2) = -1 + 3 = 2, and A(3) = -1 + 3 - 5 = -3. (Note that (-1)i is 1 if i is even and -1 if i is odd.)

Find a closed-form formula for A(n) (a simple expression with no summation) and prove it correct for all n by induction. (It is possible to do this either by ordinary or by odd-even induction.)

The closed form is A(n) = (-1)nn. We let P(n) be the statement that A(n) = (-1)nn. For the base of of the inductions, we prove P(0) by noting that A(0) is the empty sum 0, and that (-1)00 evaluates to 0.

For the inductive step of the ordinary induction, we assume P(n), that A(n) is (-1)nn, and try to prove P(n+1), that A(n+1) is (-1)n+1(n+1). By definition, A(n+1) is A(n) plus (-1)n+1 (2(n+1)-1), or A(n+1) plus (-1)n+1(2n+1). By the IH, A(n) is (-1)nn = -(-1)n+1n = (-1)n+1(-n). Plugging into our expression for A(n+1), we get (-1)n+1[-n+(2n+1)] which is (-1)n+1(n+1), as desired.

Several people got the expression and the base case right, but then offered an inductive case that never used the definition of A, and thus never expressed A(n+1) in terms of A(n).

The odd-even induction works as follows. We prove for all even n that A(n) = n. This is proved above for n = 0. To prove P(n+2) from P(n), we express A(n+2) as A(n) - (2n+1) + (2n+3), using the fact that n is even. This reduces to A(n) + 1, and since the IH says that A(n) = n, we have that A(n+2) = n+2. For odd n, we prove that A(n) = -n. We prove the base case P(1) by observing that A(1) = -1. For the inductive case, we prove P(n+2) from P(n) by expressing A(n+2) as A(n) + (2n+1) - (2n+3), using the fact that n is odd. This reduces to A(n) - 2, and since P(n) says that A(n) = -n, we have that A(n+2) = -(n+2) as desired.

• Question 2 (55): For any natural n, let b(n) be the binary string consisting of n ones with a single zero between each pair of adjacent ones. So b(0) = λ, b(1) = "1", b(2) = "101", and b(5) = "101010101". Let f(n) be the natural denoted by b(n). So f(0) = 0, f(1) = 1, f(2) = 5, and f(5) = 341.

• (a,5) Using the definitions above and your understanding of binary representation, explain why for any natural n, f(n+1) = 4n + 1.

If n is positive, we get b(n+1) from b(n) by concatenating "01" on the right of b(n). This is the same as shifting the number f(n) left by two places and then adding 1. Since shifting left doubles a binary number, we obtain f(n+1) by doubling f(n) twice and then adding 1, to get 4f(n) + 1.

• (b,20) Using the result of part (a), prove the following two statements for all naturals n by induction:
• P(n): If n is even, then f(n) ≡ 0 (mod 5)
• Q(n): If n is odd, then f(n) ≡ 1 (mod 5)

You may choose to prove ∀n:P(n) ∧ Q(n) by ordinary induction, to prove ∀n:P(n) and ∀n:Q(n) separately by odd-even induction, or to prove both by strong induction.

I think the easiest is odd-even induction. It suffices to prove P(0), Q(1), ∀n:P(n)→P(n+2), and ∀n:Q(n)→Q(n+2). P(0) and Q(1) follow from the facts that f(0) = 0 and f(1) = 1. For any n, part (a) tells us that f(n+1) = 4n+1, and thus that f(n+2) = 4f(n+1) + 1 = 16f(n) + 5. P(n) says that f(n) ≡ 0, so if P(n) is true then f(n+2) ≡ 16(0) + 5 ≡ 0 and P(n+2) is true. Q(n) says that f(n) ≡ 1, so if Q(n) is true then f(n+2) = 16f(n) + 5 ≡ 16(1) + 5 ≡ 1 (mod 5), because 16 ≡ 1 (mod 5).

The ordinary induction proves P(n+1) from Q(n) if n is odd, and proves Q(n+1) from P(n) if n is odd, using arithmetic mod 5 and the fact that f(n+1) = 4f(n) + 1.

The strong induction proves P(0), P(1), Q(0), and Q(1), then proves P(n+1) from P(n-1) and Q(n+1) from Q(n-1), as above.

• (c,10) Prove that the naturals f(3) and f(5) are relatively prime. (Hint: 21 times 16 = 336.)

We need only run the Euclidean Algorithm on f(5) = 341 and f(3) = 21. Using the hint, we observe that 341 = 16(21) + 5. Then 21 = 4(5) + 1, and because we have reached 1 we have proved that 341 and 21 are relatively prime.

• (d,10) Find integers x and y such that xf(3) + yf(5) = 1.

We carry out the inverse algorithm by writing each of the numbers in the Euclidean Algorithm as a linear combination of 341 and 21. We write 341 = 1(341) + 0(21), 21 = 0(341) + 1(21), 5 = 1(341) - 16(21), and 1 = 21 - 4(5) = -4(341) + 65(21). So we take x to be 65 and y to be -4.

• (e,5) Explain why for any positive natural n, f(n) and f(n+1) are relatively prime. (Hint: Use the Euclidean Algorithm and the result of part (a).

When we apply the Euclidean Algorithm to f(n+1) and f(n), we first divide f(n+1) by f(n). By the result of part (a), it goes into f(n+1) four times with a remainder of 1. So the next pair of numbers in the Euclidean Algorithm are f(n) and 1, and since we have reached 1 we have proved that f(n+1) and f(n) are relatively prime. (The only way this would not work is if f(n) were 0, which is why we require n to be positive.)

• (f,5) If for some natural x we are given that x ≡ 42 (mod f(3)) and x ≡ 42 (mod f(5)), what does the Chinese Remainder Theorem tell us about x?

Since we proved 341 and 21 to be relatively prime in part (c), the Chinese Remainder Theorem tells us that there exists an integer c such that x satisfies these two congruences if and only if x ≡ c (mod 7161). (Here 7161 = 341(21), though you were not required to multiply this out.)

Furthermore, we can obtain the number c. The quick way to do this is to notice that c = 42 works, since all we need is any number that is congruent to 42 both mod 21 and mod 341. If we don't notice this, we can follow the proof of the CRT (and use the inverses from part (d), and let c be (42 times 341 times -4) + (42 times 21 times 65). Of course this is 42 times (341(-4) + 21(65) = 42, using the equation found in part (d).

• Question 3 (35): This question involves strings over the alphabet {A,B,C,...,Z}. In the game of ScrabbleTM, every letter in this alphabet has a value. We define a function lab from letters to naturals such that lab(ch) is the value of ch. (For example, lab(A) = 1 and lab(X) = 8.)

If w is a string of letters, we define the score of w by two rules:

• The score of λ is 0.
• If x is any string and a is any letter, the score of xa is the score of x plus lab(a).

• (a,15) Prove that for any strings u and v, the score of cat(u,v) is the score of u plus the score of v. Recall the definition of the function cat: For any strings w and x and any letter a, cat(w, λ) = w and cat (w,xa) = cat(w,x)a. (Hint: Let u be arbitrary and use string induction on v.)

As suggested, we let u be arbitrary and use string induction on v. The base case is v = λ, and in this case the score of cat(u,λ) is the score of u by the definition of the cat function, and this equals the score of u plus the score of λ because the latter is 0 by the definition of score.

For the inductive case, we let a be an arbitrary letter, let v be an arbitrary string, assume P(v) (that score(cat(u,v)) = score(u) + score(v)) and set out to prove P(va) (that score(cat(u,va)) = score(u) + score(va). By the definition of cat, score(cat(u,va)) = score(cat(u,v)a). By the definition of score, this is score(cat(u,v)) + lab(a). By the IH, this is score(u) + score(v) + lab(a) = score(u) + [score(v) + lab(a)] which is score(u) + score(va) by the definition of score. We have proved P(va) from P(v) for arbitrary v and a, completing the string induction.

• (b,10) Write a recursive pseudo-Java method `scoreOf` that takes a string w and returns the score of w. You may assume that a method `lab` has already been defined as above. Do not worry about error handling, such as for invalid characters in w.

``````      public natural scoreOf (string w)
{// Returns Scrabble score of w as defined by lab
if (w == emptystring) return 0;
return scoreOf(allButLast(w)) + lab(last(w));}
``````

• (c,10) Argue that for any string w, your method in part (b) returns the score as defined above. (This will be a string induction.)

For the base case, we must show that the method returns the score of λ, which is defined to be 0, on input λ. This is true because the third line of the code checks for w = λ and returns 0 if it is.

For the inductive case, we let v be an arbitrary string, let a be an arbitrary letter, and assume that `scoreOf(v)` terminates and returns the correct score of v. Given these assumptions, we consider what happens when `scoreOf` is called with input va. We reach the last line, compute `allButLast(va)` to be v and `last(va)` to be a, and then return `scoreOf(v) + lab(a)`. By the IH, this is the correct score of v plus lab(a), and by the definition of score this is the correct score of va. So `scoreOf(va)` returns the correct answer and we have completed the string induction.