Download CS 40: Final Examination - UCSB Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Line (geometry) wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
CS 40: Final Examination
Department of Computer Science
University of California, Santa Barbara
Closed-Book, 3 hours
Winter 2008
Instructions
• Before you answer any questions, print your name and perm number.
• Read each question carefully. Make sure that you clearly understand each question before
answering it.
• Put your answer to each question on its own page.
• You may wish to work out an answer on scratch paper before writing it on your answer page;
answers that are difficult to read may lose points for that reason.
• You may not leave the room during the examination, even to go to the bathroom.
• You may not use any personal devices, such as calculators, PDAs, or cell phones.
1
1. (25 points) Define the following terms:
contrapositive of p → q: ¬q → ¬p
direct proof of p → q: a proof that p → q is true that proceeds by showing that q must be
true when p is true
loop invariant: a property that remains true during every traversal of a loop.
initial assertion: the statement specifying the properties of the program’s input values.
r-permutation: an ordered arrangement of r elements of a set
Pascal’s Identity: a Ãrepresentation
of the binomial coefficients where the ith row of the
!
i
triangle contains
, for j = 0, 1, 2, . . . , i.
j
recurrence relation: a formula expressing terms of a sequence, except for some initial
terms, as a function of 1 or more previous terms of the sequence
divide-and-conquer algorithm: an algorithm that solves a problem recursively by splitting it into a fixed number of smaller problems of the same type
2
2. (5 points) Give a decimal number for each of the following:
(a) P (10, 3) = 10!/7! = 10 · 9 · 8 = 720.
(b) C(10, 7) = C(10, 3) = 10!/(3!7!) =
10·9·8
3·2·1
3
= 120.
3. (8 points) Prove by induction that 1 + 3 + · · · + (2n + 1) = (n + 1)2 .
Basis n = 0: 1 = (2 · 0 + 1) = (0 + 1)2 .
Induction hypothesis: 1 + 3 + · · · + (2n + 1) = (n + 1)2 .
To show: 1 + 3 + · · · + (2(n + 1) + 1) = ((n + 1) + 1)2 .
1 + 3 + · · · + (2(n + 1) + 1) =
(1)
1 + 3 + · · · + (2n + 1) + 2n + 3 =
(2)
2
(n + 1) + 2n + 3 =
(3)
n + 2n + 1 + 2n + 3 =
(4)
2
2
n + 4n + 4 =
2
(n + 2)
= ((n + 1) + 1)
Step 3 follows from step 2 by the induction hypothesis.
4
(5)
2
(6)
4. (8 points) Prove that every amount of postage of 35 cents or more can be formed using just
5-cent and 9-cent stamps.
Let P (n) denote the statement that n cents of postage can be formed using just 5-cent and
9-cent stamps. We show that ∀n ≥ 35, P (n), using strong mathematical induction:
Basis P (35): Use 7 5-cent stamps.
To show: If P (35) ∧ P (36) ∧ · · · ∧ P (n) then P (n + 1).
Case P (n) used at least 1 9-cent stamp: Replace a 9-cent stamp with 2 5-cent stamps,
producing n + 1 cents of postage.
Case P (n) used 0 9-cent stamps: Since n ≥ 35, at least 7 5-cent stamps were used to
produce n cents of postage. Replace 7 5-cent stamps with 4 9-cent stamps, producing
n + 1 cents of postage.
5
5. (8 points) Let the set A of bit strings be defined recursively by
λ∈A
0x1 ∈ A if x ∈ A,
where λ is the empty string. Give a simple description of the strings in A. That is, describe
the form of the strings in A.
When the string is of the form 0n 1n , for n ≥ 0, where 0n denotes the string of n 0s, and 1n
denotes the string of n 1s.
6
6. (7 points)
(a) How many 5-letter “words” (sequences of an 5 letters with repetition) are there?
265 .
(b) How many with no repeated letters?
26 · 25 · 24 · 23 · 22.
7
7. (6 points) How many arrangements are there of the 8 letters in the word VISITING?
8!
.
3!
8
8. (6 points) What is the coefficient of x8 y 9 in the expansion of (3x + 2y)17 ?
Let w = 3x and z = 2y. The coefficient of w8 z 9 in (w + z)17 is C(17, 8): C(17, 8)w8 z 9 =
C(17, 8)38 x8 29 y 9 . The coefficient we seek thus is C(17, 8)38 29 .
9
9. (6 points) How many ways are there to pick a collection of 8 coins from piles of pennies,
nickels, dimes, and quarters?
C(8 + 4 − 1, 4 − 1) = C(8 + 4 − 1, 8).
10
10. (7 points) Suppose that the number of bacteria in a colony triples every hour.
(a) Give a recurrence relation for the number of bacteria after n hours.
bn = 3bn−1 .
(b) If 100 bacteria are used to begin a colony, how many bacteria will be in the colony after
6 hours?
b0 = 100
b1 = 3b0 = 3 · 100 = 300
b2 = 3b1 = 3 · 300 = 900
b3 = 3b2 = 3 · 900 = 2700
b4 = 3b3 = 3 · 2700 = 8100
b5 = 3b4 = 3 · 8100 = 24300
b6 = 3b5 = 3 · 24300 = 72900
11
Master Theorem: Let f be an increasing function that satisfies the recurrence relation
f (n) = af (n/b) + cnd
whenever n = bk , where k is a positive integer, a ≥ 1, b > 1 is an integer, and c and d are
real numbers with c positive and d nonnegative. Then

d

 O(n )
if a < bd ,
O(nd logn) if a = bd }
f (n) is

 O(nlogb a )
if a > bd .
11. (6 points) Let f (n) = 2f (n/3) + 4, with f (1) = 1, and n = 3k . Give a big-O estimate for
f (n), assuming that it is an increasing function.
Since 2 > 30 , f (n) is O(nlog3 2 ).
12
12. (8 points) “If a set of 7 integers are selected from the first 10 positive integers, then it must
contain 2 pairs of integers that sum to 11.” True or false? Prove your answer.
True.
Partition the set into 5 pigeon holes: {1, 10}, {2, 9}, {3, 8}, {4, 7}, {5, 6}. Place the 7 selected
integers into these 5 pigeon holes. Since there are only 5 pigeon holes, at least 2 pigeon holes
must have 2 of the selected numbers. The 2 numbers in a pigeon hole sum to 11.
13