
CPSC 5115 Algorithm Analysis and Design
Homework Set 3 Due Friday, November 16, 2007
Homework key with answers and comments.
1. Find the greatest common divisor and least common multiple of the
following two pairs of numbers. Use Euclid’s Algorithm for GCD.
a) 105 and 231
b) 13 and 17
ANSWER: The least common multiple of two integers is related to their greatest common
divisor by a simple formula: LCM(M, N) = (MN) / GCD(M,N). Apply Euclid’s Algorithm
to get the GCD (Greatest Common Divisor).
a) GCD(231, 105) = GCD (105, 21) 231 = 2105 + 21
GCD(105, 21) = GCD (21, 0) = 21 105 = 521
LCM(231, 105) = (231105) / 21 = 2315 = 1,155.
b) GCD(17, 13) = GCD(13, 4) 17 = 113 + 4
GCD(13, 4) = GCD(4, 1) 13 = 34 + 1
GCD(4, 1) = GCD(1, 0) = 1 4 = 41 + 0
LCM(17, 13) = (1713) / 1 = 221.
NOTE: Each of 13 and 17 are prime numbers, so the pair must be relatively prime.
It follows immediately that their greatest common divisor is 1 and that their
product is their least common multiple. I just wanted to be complete.
2. Construct the shift table from Horspool’s Algorithm for the following string
TCCTATTCTT. This 10–character string from problem 2, section 7.2.
ANSWER: First view the pattern as an array of characters: P[0, 9].
Index 0 1 2 3 4 5 6 7 8 9
Char T C C T A T T C T T
M = 10, M – 1 = 9, M – 2 = 8. For J = 0 to 8 Do Table[P[J]] = 9 – J
J = 0 9 – J = 9 P[0] = “T” Table[“T”] = 9
J = 1 9 – J = 8 P[1] = “C” Table[“C”] = 8
J = 2 9 – J = 7 P[2] = “C” Table[“C”] = 7
J = 3 9 – J = 6 P[3] = “T” Table[“T”] = 6
J = 4 9 – J = 5 P[4] = “A” Table[“A”] = 5 Table[“A”] = 5
J = 5 9 – J = 4 P[5] = “T” Table[“T”] = 4
J = 6 9 – J = 3 P[6] = “T” Table[“T”] = 3
J = 7 9 – J = 2 P[7] = “C” Table[“C”] = 2 Table[“C”] = 2
J = 8 9 – J = 1 P[8] = “T” Table[“T”] = 1 Table[“T”] = 1, but 0 is OK
All other characters have a shift value of 10, the length of the pattern.