# Second Midterm Exam

### Directions:

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

```  Q1: 20 points
Q2: 55 points
Q3: 35 points
Total: 110 points
```

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

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

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

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

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

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

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

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

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