0% found this document useful (0 votes)
5 views

Exam #2 Study Questions (solution)

The document contains practice questions and solutions for mathematical induction, recursive and non-recursive algorithms, and combinatorial problems. It includes proofs for sequences, algorithms for summing primes, counting primes, and calculating probabilities in various scenarios. Additionally, it discusses the use of the Master Theorem and provides estimates for the number of multiplications in algorithms.

Uploaded by

Serdar Turan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Exam #2 Study Questions (solution)

The document contains practice questions and solutions for mathematical induction, recursive and non-recursive algorithms, and combinatorial problems. It includes proofs for sequences, algorithms for summing primes, counting primes, and calculating probabilities in various scenarios. Additionally, it discusses the use of the Master Theorem and provides estimates for the number of multiplications in algorithms.

Uploaded by

Serdar Turan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exam #2 Practice Questions (solutions)

5.1 Use mathematical induction to prove the following. Do not use any variables besides “n” in your proof.
Especially, avoid “k”.
1 1 1 1
a) + + ⋅ ⋅ ⋅ + n = 1 − n for n ≥ 1.
2 4 2 2
1 1
1
=1− 1 ✓
2 2
1 1 1 1
Assume + + ⋅ ⋅ ⋅ + n−1 = 1 − n−1 .
2 4 2 2
1 1 1 1 1 1
+ + ⋅ ⋅ ⋅ + n−1 + n = 1 − n−1 + n .
2 4 2 2 2 2
1 1 1 −2 1 −2 + 1 1
+ + ⋅⋅⋅ + n = 1 + n + n = 1 + = 1 − n.
2 4 2 2 2 2n 2
QED

b) If S1 = 21 , and Sn = Sn−1 + 1
2n
for n > 1, then Sn = 1 − 1
2n
for all n ≥ 1.
1 1
S1 = = 1 − 1 ✓
2 2
1
Assume n > 1 and Sn−1 = 1 − n−1
.
2
1 1 1 2 1 −2 + 1 1
Then Sn = Sn−1 + n = 1 − n−1 + n = 1 − n + n = 1 + n =1− n
2 2 2 2 2 2 2
QED

5.2 a) Use strong induction to prove that if n ≥ 12, then n = a ⋅ 4 + b ⋅ 5 for some a, b ∈ N. Do not use the variable
“k”, or any other variable besides “n” in your proof.
12 = a ⋅ 4 + b ⋅ 5 (a = 3, b = 0)
13 = a ⋅ 4 + b ⋅ 5 (a = 2, b = 1)
14 = a ⋅ 4 + b ⋅ 5 (a = 1, b = 2)
15 = a ⋅ 4 + b ⋅ 5 (a = 0, b = 3)
Assume n − 4 = a ⋅ 4 + b ⋅ 5 for some a, b ∈ N
Then n = 4 + a ⋅ 4 + b ⋅ 5 = (a + 1) ⋅ 4 + b ⋅ 5 = a ⋅ 4 + b ⋅ 5 (a = a + 1)
′ ′

QED
b) a1 = −1, a2 = 2, an = − (2an−1 + an−2 ) if n > 2.
i) Calculate a3 , a4 and a5 .
a3 = − (2 ⋅ 2 + (−1)) = −3
a4 = − (2 ⋅ (−3) + 2) = 4
a5 = − (2 ⋅ 4 + (−3)) = 5
ii) Make a conjecture about the value of an . (Hint: your answer will have a factor of (−1) .)
n

Conjecture: an = (−1) n.
n

iii) Use strong induction to prove that your conjecture is correct.


a1 = −1 = (−1) ⋅ 1 ✓
1

a2 = 2 = (−1) ⋅ 2 ✓
2

Assume an−1 = (−1) (n − 1) and an−2 = (−1) (n − 2),


n−1 n−2

an = − (2 ⋅ (−1) (n − 1) + (−1) (n − 2))


n−1 n−2

= − (2 ⋅ (−1) (−1) (n − 1) + (−1) (−1) (n − 2))


n −1 n −2

= − (−2 ⋅ (−1) (n − 1) + (−1) (n − 2))


n n

= −(−1) (−2(n − 1) + (n − 2))


n

= −(−1) (−2n + 2 + n − 2)
n

= −(−1) (−n)
n

= (−1) n
n

QED

A.3/3.1 Write non-recursive algorithms for the following.

a) Given a list of integers a1 , a2 , . . . an , find the sum of the ones that are prime.
procedure sum of primes(a1 , a2 , . . . an , integers)
accum = 0
for i = 1 to n
if ai is prime
accum = accum + ai
return accum

b) Given a list of integers a1 , a2 , . . . an , find the number of primes in the list.


procedure number of primes (a1 , a2 , . . . an , integers)
accum = 0
for i = 1 to n
if ai is prime
accum = accum + 1
return accum

c) Given a list of integers a1 , a2 , . . . an , find the index of the last prime. Return 0 if there are no primes.
procedure index of last prime(a1 , a2 , . . . an , integers)
accum = 0
for i = 1 to n
if ai is prime
accum = i
return accum
5.4 Write recursive algorithms for the following. You may assume that the language includes all of the list
operations defined in class.

a) Given L, a list of integers, find the sum of the ones that are prime.
procedure sum of primes(L: list of integers)
if L is empty
return 0
if first(L) is prime
return first(L) + sum of primes(rest(L))
return sum of primes(rest(L))

b) Given L, a list of integers, find the number of primes in the list.


procedure number of primes(L: list of integers)
if L is empty
return 0
if first(L) is prime
return 1 + number of primes(rest(L))
return number of primes(rest(L))

c) Given n, return an , where an is nth term of the sequence defined in 5.2 b)


procedure a(n: positive integer)
if n = 1
return −1
if n = 2
return 2
return − (2a(n − 1) + a(n − 2))

3.2 Find k and C to witness each of the following:

Instructor’s Note: There are many possible choices for C and k. The ones below are my choices. Other answers
are also acceptable.

a) 5x + 4x + 3x + 2 is O (x )
3 2 3

3 3
x ≥ 0 → 5x ≤ 5x
2 3
x ≥ 4 → 4x ≤ x
3
x ≥ 2 → 3x ≤ x
3
x≥2→2≤x
3 2 3
x ≥ 4 → 5x + 4x + 3x + 2 ≤ 8x
k = 4, C = 8

b) ln x is O (log10 (x)). (Hint: Google ”change of base formula for logarithms”)


1
ln(x) = log10 (x)
log10 (e)
1
k = 1, C =
log10 (e)

c) n is O ( 12 n ).
2 3
2 3
n ≤ C ⋅ 12 n → 1 ≤ C ⋅ 12 n → 2 ≤ C ⋅ n
k = 2, C = 1
d) log (n ) is O (log(n )).
3 2

log (n ) = 3 log (n) = ⋅ 2 log (n) = 23 log (n )


3 3 2
2
3
k = 1, C = 2

3.3 Give a Θ estimate for the number of multiplications


procedure some procedure (some inputs)
accum = 0
for i = 1 to 2n
2
for j = 1 to 3n
for k = 1 to 100
accum = accum + i ⋅ j ⋅ k
return accum

The number of multiplications is 2n ⋅ 3n ⋅ 100 ⋅ 2 = 1200n which is in Θ (n ) .


2 3 3
8.3
Give a Θ estimate for the number of multiplications.
procedure g(n: positive integer)
if n = 1
return 1
else
accum = 1
4
for i = 1 to 2n
5
for j = 1 to 3n
accum = accum + i ⋅ j
return accum + g ( n3 ) + g ( n3 )

The parameters for the Master Theorem are a = 2, b = 3, c = 2 ⋅ 3 = 6, d = 9. a < b Θ (n )


d 9

6.1 Of all of the bitstrings of length 6,


a) How many begin with “11”?
4
2

b) How many begin with “11” and end with “00”?


2
2

c) How many begin with “11” or end with “00”, or both?


4 4 2
2 +2 −2

d) How many begin with “11” or end with “00”, but not both?
Start with “11”, but doesn’t end with “00”.
i) Choose the first two bits (1 choice: 11)
ii) Choose the middle two bits (4 choices: 00, 01, 10 or 11)
iii) Choose the last two bits (3 choices: 01, 10 or 11)
1⋅4⋅3

Doesn’t start with “11”, but ends with “00”.


i) Choose the first two bits (3 choice: 00, 01, 10)
ii) Choose the middle two bits (4 choices: 00, 01, 10 or 11)
iii) Choose the last two bits (1 choice: 00)
3⋅4⋅1
1⋅4⋅3+3⋅4⋅1
6.3 You have 10 history books, 15 novels and 5 math books. All 30 books are different.
a) How many ways can you loan 3 history books, 7 novels and 2 math books to a friend?
10 15 5
( 3 )( 7 )(2)

b) How many ways can you place 3 history books, 7 novels and 2 math books on a shelf, if the history books
come first, the novels are in the middle, and the math books come last?
P (10, 3)P (15, 7)P (5, 2)

c) How many ways can you place 3 history books, 7 novels and 2 math books on a shelf, if the history books
must all be grouped together, the novels must be grouped together, and the math books must be grouped together?
P (3, 3)P (10, 3)P (15, 7)P (5, 2)

d) How many ways can you place 3 history books, 7 novels and 2 math books on a shelf?
10 15 5
( 3 )( 7 )(2)P (12, 12)

7.1 a) A fair two-sided coin is flipped 6 times. What is the probability that
i) heads will come up exactly 4 times?
(64)
26

ii) heads will come up at least 4 times?


(64) + (65) + (66)
26

iii) heads will come up at most 3 times?


2 − ((64) + (65) + (66))
6

26

b) A fair three-sided coin is flipped 6 times. What is the probability that


i) heads will come up exactly 4 times?
(64) ⋅ 1 ⋅ 2
4 2

36

ii) heads will come up at least 4 times?


(64) ⋅ 1 ⋅ 2 + (65) ⋅ 1 ⋅ 2 + (66) ⋅ 1 ⋅ 2
4 2 5 1 6 0

36

iii) heads will come up at most 3 times?


3 − ((64) ⋅ 1 ⋅ 2 + (65) ⋅ 1 ⋅ 2 + (66) ⋅ 1 ⋅ 2 )
6 4 2 5 1 6 0

36
c) The game Mega-Millions is played as follows:
Players pick six numbers from two separate pools - five different numbers from 1 to 70 (the white numbers)
and one number from 1 to 25 (the gold numbers).
What is the probability that you match exactly three white numbers and don’t match the gold number?
(53)(65
2
)(24
1
)
(70
5
)(25
1
)

d) Recall that a deck of cards is defined to be the set Ranks × Suits, where Ranks = {A, 2, 3, . . . , J, Q, K}
(#Ranks = 13), and Suits = {♣, ♦, ♠, ♥} (#Suits = 4.) A poker hand is five cards from the deck. A Flush
House is three cards of the same suit, and two cards of a different matching suit.
What is the probability that a poker hand is a Flush House?
(41)(13
3
)(31)(13
2
)
(52
5
)

6.5/8.6 You have a bowl with three types of candies: cherry, orange and pineapple.
a) How many ways to pick 15 candies?
2 bars, 15 stars: (17
15
)

b) How many ways to pick 15 candies, with at least two of each flavor?
15 − 2 ⋅ 3 = 9 stars, 2 bars: (11
9
)

c) How many ways to pick 110 candies with at least 10 cherry candies, at most 19 orange candies, and at most
29 pineapple candies?
First pick the 10 cherry candies. This leaves 100 more candies to pick, with at most 19 orange candies, and at
most 29 pineapple candies?
Three types of candies ↔ 2 bars.
102
Pick 100 candies, no restrictions: 2 bars, 100 stars: (100)

82
Pick 100 candies, at least 20 orange: 2 bars, 80 stars: (80)

72
Pick 100 candies, at least 30 pineapple: 2 bars, 70 stars: (70)

52
Pick 100 candies, at least 20 orange, at least 30 pineapple: 2 bars, 50 stars: (50)

102 82 72 52
(100) − [(80) + (70) − (50)]

You might also like