Professional Documents
Culture Documents
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
(b) Use the insertion sort to sort the sequence above, showing the lists obtained at
each step
(c) Use the selection sort to sort the sequence above, showing the lists obtained at each
step
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.
(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