Download Test 2 solutions

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

Approximations of π wikipedia , lookup

Positional notation wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
Compsci 230
Exam 2 Solutions
Nov. 19, 2013
Dr. Rodger
1. (3 pts) Show that 3x2 + 2x + 10 is O(x2 ). Be sure to specify a C and k.
ANSWER:
3x2 + 2x + 10 ≤ 10x2 + 10x + 10 ≤ 10x2 + 10x2 + 10x2 = 30x2
thus C=30, k=1 since 3x2 + 2x + 10 ≤ 30x2 for x > 1
ALT: 3x2 + 2x + 10 ≤ 3x2 + 2x2 + 10x2 = 15x2 for x > 1, so
C=15, k=1 also is true
2. (4 pts) Give the best big-O for the following.
(a) 2x4 + 3x3 + 30x2 + 20000x + 1 is O(?)
ANSWER:
O(x4 )
(b) 3(log2 x)3 + 2x is O(?)
ANSWER:
O(x)
Note the difference between x and log2 x. As x doubles in size,
log2 x gets bigger by 1.
x is 2, log2 x is 1.
x is 4, log2 x is 2.
x is 8, log2 x is 3.
x is 1024, log2 x is 10.
As x gets bigger and bigger, x will dominate (log2 x)3 .
3. (4 pts) Complexity of Problems
For each of the problems below. Describe if they are tractable, intractable or unsolvable
or explain why there is not enough information available to make a decision.
(a) Fred has designed an algorithm to solve Problem A that runs in O(3n ) time. What
can you say about problem A?
ANSWER:
We can’t say much about the problem. We don’t know much about Fred, but
he has designed a solution that is not polynominal. We don’t know if he
just hasn’t found a polynomial solution yet. The problem might be
intractable. But maybe there is a polynominal time solution, Fred
just didn’t think of it. There is not enough information.
1
(b) Martha has designed an algorithm to solve Problem B that runs in O(n3 ) time.
What can you say about problem B?
ANSWER:
This problem is tractable because Martha has found a polynomial time solution.
4. (6 pts) Working with different bases.
(a) Convert the decimal number 58 to a hexidecimal number (base 16).
ANSWER:
(58)10 = 3 ∗ 161 + 10 ∗ 160 = (3A)16
(b) Convert binary number (10111001101111)2 to a hexidecimal number (base 16).
ANSWER:
(10111001101111)2 = (0010 1110 0110 1111)2 = (2E6F )16
(c) Compute (211)3 + (202)3 with the result in base 3.
ANSWER:
(1120)3
5. (4 pts) Mod problems
(a) What is (46 mod 8) mod 5?
ANSWER:
(46 mod 8) mod 5 = 6 mod 5 = 1
(b) Give two values of x such that x ≡ 17(mod 10).
ANSWER:
27-17 is divisible by 10, and 37-17 is divisible by 10 so x=27 and
x=37. There are several other possible answers.
6. (6 pts) Express the gcd(65, 150) as a linear combination of 65 and 150 by first finding
the gcd using the Euclidean algorithm and then solving for the Bezout coefficients.
Please make it clear what the coefficents are.
ANSWER:
150 = 2 * 65 + 20
65 = 3 * 20 + 5
20 = 4 * 5
The gcd(65, 170) = 5
5 = 65 - 3 * 20
20 = 150 - 2 * 65
Thus 5 = 65 - 3 * [150 - 2 * 65] by substitution
5 = 7 * 65 - 3 * 150
Bezout coefficients are 7 and -3.
2
7. (9 pts) Prime Numbers
(a) What is the greatest common divisor of 150 and 180?
ANSWER:
150 = 2 ∗ 3 ∗ 52
180 = 22 ∗ 33 ∗ 5
GCD = 2 ∗ 5 ∗ 3 = 30
(b) What is the least common multiple of 150 and 180?
ANSWER:
LCD = 22 ∗ 33 ∗ 52 = 900
(c) Are the numbers 8, 9 and 35 pairwise relatively prime? Explain.
ANSWER:
8 = 23 , 9 = 32 , 35 = 5 ∗ 7
The gcd(8,9)=1, gcd(8,35)=1, gcd(9,35)=1. So yes, they are pairwise
relatively prime since the gcd of every pair is 1.
8. (6 pts) Jill has created a check digit for her order numbers. The order numbers are 5
digits total with each of the first four digits being one of 0-9. The check digit is the
5th digit and is the (sum of the first 4 digits) mod 5.
(a) What is the 5th digit, the check digit, if the first 4 digits are 7241?
ANSWER:
7+2+4+1 = 14 mod 5 = 4
(b) The 2cd digit is smudged and unreadable, shown here as a Q in 3Q482. If the
other numbers are correct, what could Q be?
ANSWER:
(3 + Q + 4 + 8) mod 5 = 2
(15 + Q) mod 5 = 2
Q = 2 or Q = 7
9. (4 pts) A variant of the Caeser cipher assigns digits to letters in the following manner
0 to A, 1 to B, etc., as shown below, and then uses f(p) = (p + 6) mod 26 as the
encryption method.
0
A
1
B
2
C
3
D
4
E
5
F
6
G
7
H
8
I
9
J
10
K
11
L
12
M
13
N
14
O
15
P
16
Q
17
R
18
S
19
T
20
U
21
V
22
W
23
X
The message HEK SOGSO was encrypted using this variant. What is the original
message?
ANSWER:
H = 9 - 6 is 3 which is B
3
24
Y
25
Z
E is 4 - 6 is -2 or 24 which is Y
K is 10 - 6 is 4 which is E
S is 18 - 6 is 12 which is M
O is 14 - 6 is 8 which is I
G is 6 - 6 is 0 which is A
BYE MIAMI
10. (5 pts) Prove by mathematical induction 12 + 22 + 32 + . . . + n2 = n(n + 1)(2n + 1)/6
for n ≥ 1.
ANSWER:
Basis: (n=1) 12 = 1(2)(3)/6 = 6/6 = 1 Check
Induction Step:
First state the Induction Hypothesis (I.H.), Assume true for arbitrary positive k,
12 + 22 + 32 + . . . + k 2 = k(k + 1)(2k + 1)/6 for k ≥ 1.
Show true for k+1, which means show
12 + 22 + 32 + . . . + k 2 + (k + 1)2 = (k + 1)(k + 2)(2k + 3)/6 is true
Start with 12 + 22 + 32 + . . . + k 2 + (k + 1)2
= k(k + 1)(2k + 1)/6 + (k + 1)2 by I.H. [Be sure to identify using I.H. here]
= k(k + 1)(2k + 1)/6 + 6(k + 1)2 /6
= [(k + 1)(k(2k + 1) + 6(k + 1)]/6
= [(k + 1)(2k 2 + 7k + 6)]/6
= [(k + 1)(k + 2)(2k + 3)]/6
11. (5 pts) Suppose that the only postage is 3 cent stamps and 4 cent stamps.
(a) What is the smallest value p such that postage can be made for every value p and
greater?
ANSWER:
p=6
(b) What value(s) do you need to prove for the basis in order to do a strong proof by
induction? Show the basis is true.
ANSWER:
p=6, two 3-cent stamps
p=7, 3-cent and 4-cent stamp
p=8, two 4-cent stamps
(c) Complete the strong induction proof that every postage amount p and greater for
the p you got in part a could be made with these two types of stamps.
4
ANSWER:
Basis: (is part b - P(6), P(7) and P(8) is true)
Inductive Step: I.H. is P(j), that we can form postage for j with 3-cent
and 4-cent stamps for 6 ≤ j ≤ k, where k is an integer, k ≥ 8.
[Note strong induction is that we assume P(6) and P(7) and P(8) and ... and P(K)
are all true, then we show they imply P(k+1) is true].
Show P(K+1) is true, that postage for k+1 cents can be formed with
3-cent and 4-cent stamps.
P(k+1-3) = P(k-2), and P(k-2) is true by the I.H. since k − 2 ≥ 6, and k >= 8
P(k+1) is the same as P(k-2) plus a 3-cent stamp
Thus, P(k+1) can be formed with 3-cent and 4-cent stamps, so P(k+1) is true.
12. (3 pts) Suppose the Duke Blue Devil will wear one of his two heads, one of his 6 shirts
and one of his 4 pairs of pants. How many different outfits can the Duke Blue Devil
wear?
ANSWER:
2 * 6 * 4 = 48
13. (6 pts) Consider a bit string of length 6 formed with only 0’s and 1’s.
(a) How many possible unique strings are there?
ANSWER:
26
(b) How many strings are there with at least three 1’s?
ANSWER:
6
6
6
6
+
+
+
= 20 + 15 + 6 + 1 = 22
3
4
5
6
OR
6
6
6
2 −
−
−
= 64 − 15 − 6 − 1 = 22
0
1
2
6
14. (6 pts) How many strings are there of five uppercase letters A-Z such that (assume
letters can repeat unless it specifically says they cannot):
(a) the letter D only appears once and appears as the first letter?
ANSWER:
254
5
(b) the letter X can never be followed immediately by the letter Y (AXYBC is not
valid but AXBYC, AYXBC and AABBC are valid).
ANSWER:
265 − 4 ∗ 263 + 3 ∗ 26
All possible combinations of letters
, XY ,
XY,
X Y),
minus 4 locations that XY appears (X Y
and the other three spots are any letter
plus 3 cases where you have two XY pairs and one letter left (those
cases got subtracting twice with the second term, so add one of them
back in), Those are (XYXY , XY XY, and XYXY).
15. (6 pts) Consider the string BEETLELOVE.
(a) How many different strings of length 10 can be made using the letters in BEETLELOVE?
ANSWER:
10!/(4!*2!)
(b) How many different strings of length 10 can be made using the letters in BEETLELOVE
if the four E’s must be adjacent to each other?
ANSWER:
7!/2! = 12
16. (3 pts) What is the coefficient of x5 y 2 in (x + y)7 ?
ANSWER:
[7 2] = [7 5] = = 7!/(2!*5!) = 21
17. (3 pts) We have ten people of which only one person is wearing a red shirt. How many
different ways are there to arrange 5 of the people in a line from the 10 people if the
person wearing the red shirt must be included?
ANSWER:
5 * 9 * 8 * 7 * 6 = 9!/4! = [9 5] * 5!
6