You are on page 1of 4

EE 2060 Discrete Mathematics

Midterm2 Solution
1. There is a sequence 6, 3, 11, 15, 4, 8, 1, 7, 9, 2
(a) Use the bubble sort to sort the sequence above, showing the lists obtained at each
step

At the end of the first pass: 3,6,11,4,8,1,7,9,2,15


at the end of the second pass: 3,6,4,8,1,7,9,2,11,15
at the end of the third pass: 3,4,6,1,7,8,2,9,11,15
at the end of the fourth pass:3,4,1,6,7,2,8,9,11,15
at the end of the fifth pass: 3,1,4,6,2,7,8,9,11,15
at the end of the sixth pass: 1,3,4,2,6,7,8,9,11,15
at the end of the seventh pass: 1,3,2,4,6,7,8,9,11,15
at the end of the eighth pass: 1,2,3,4,6,7,8,9,11,15
at the end of the ninth pass:1,2,3,4,6,7,8,9,11,15

(b) Use the insertion sort to sort the sequence above, showing the lists obtained at
each step

At the end of the first pass: 3,6,11,15,4,8,1,7,9,2


at the end of the second pass: 3,6,11,15,4,8,1,7,9,2
at the end of the third pass: 3,6,11,15,4,8,1,7,9,2
at the end of the fourth pass: 3,4,6,11,15,8,1,7,9,2
at the end of the fifth pass: 3,4,6,8,11,15,1,7,9,2
at the end of the sixth pass: 1,3,4,6,8,11,15,7,9,2
at the end of the seventh pass: 1,3,4,6,7,8,11,15,9,2
at the end of the eighth pass: 1,3,4,6,7,8,9,11,15,2
at the end of the ninth pass: 1,2,3,4,6,7,8,9,11,15

(c) Use the selection sort to sort the sequence above, showing the lists obtained at each
step

At the end of the first pass: 1,6,3,11,15,4,8,7,9,2


at the end of the second pass: 1,2,6,3,11,15,4,8,7,9
at the end of the third pass: 1,2,3,6,11,15,4,8,7,9
at the end of the fourth pass: 1,2,3,4,6,11,15,8,7,9
at the end of the fifth pass: 1,2,3,4,6,11,15,8,7,9
at the end of the sixth pass: 1,2,3,4,6,7,11,15,8,9
at the end of the seventh pass: 1,2,3,4,6,7,8,11,15,9
at the end of the eighth pass: 1,2,3,4,6,7,8,9,11,15
at the end of the ninth pass: 1,2,3,4,6,7,8,9,11,15
2.
(a) (n + 2 )(3 + 2) = 4 + 5 + 2 + 22
The dominant term is O(5 )

(b) The dominant term of (n! + 2 ) is O( )


The dominant term of (3 + log(2 + 1)) is O(3 )
So, (n! + 2 )(3 + log(2 + 1)) O( 3 ) = O(+3 )

3.
(a) On the initial call to the algorithm: n=3 , c=2 , 0 = 1 , 1 = 1 , 2 = 2 ,
= 3 = 3.
First iteration(i:=1)
y := y c + an1= 3*2+2 = 3*2+2 = 8

Second iteration(i:=2)
y := y c + an2= 8*2+1 = 8*2+1 = 17

Third iteration(i:=3)
y := y c + an3= 17*2+0 = 17*2-1 = 33

(b) The loop iterates n times. In one iteration of the loop: 1 multiplication is done
1 addition is done. The total number of multiplications and additions done is n
additions and n multiplications.

4.
7
(a) 3x 7(mod 31) x (mod 31), inverse of 3 = 21(mod 31)
3
x 7 21(mod 31) 147(mod 31) 23(mod 31)
97
(b) 6x 97(mod 125) x 6 (mod 125), inverse of 6 = 21(mod 125) x 97
21(mod 125) 2037(mod 125) 37(mod 125)

5.
From the description,
the CRT Theorem can be applied to find the smallest number of gold coins.

Let x be the smallest number of golds coin.


3(mod 17), 10(mod 16), 0(mod 15)
and
x a1M1 y1 a2 M 2 y2 a3 M 3 y3 (mod 17*16*15)
next, let m=15*16*17=4080
4080
a1 3, M 1 240
17
y1 * M 1 1(mod 17) y1 8(mod 17) 9(mod 17)
4080
a2 10, M 2 255
16
y2 * M 2 1(mod 16) y2 1(mod 16) 15(mod 16)
a3 0
As the result,
x a1M 1 y1 a2 M 2 y2 a3 M 3 y3 (mod 4080)
3* 240*9 10* 255*15(mod 4080)
44730(mod 4080) 3930(mod 4080)
6.
p 23, a 5, k1 8, k2 5
(a)
a k1 (mod p) 58 (mod 23),52 (mod 23) 2(mod 23)
58 (mod 23) 52 (mod 23) 24 (mod 23) 16(mod 23)
4

(b)
a k2 (mod p) 55 (mod 23),52 (mod 23) 2(mod 23)
55 (mod 23) 52 *5(mod 23) 22 *5(mod 23) 20(mod 23)
2

(c )
from Bob
16k2 (mod 23) 165 (mod 23)
162 (mod 23) 3(mod 23)
165 (mod 23) 162 *16(mod 23) 32 *16(mod 23)
2

144(mod 23) 6(mod 23)


from Alice
20k1 (mod 23) 208 (mod 23)
202 (mod 23) 9(mod 23)
208 (mod 23) 202 (mod 23) 94 (mod 23)
4

6561(mod 23) 6(mod 23)

You might also like