math 2320 assignment 1 solution

8
Math 2320 Assignment 1 Solution 1. Specify the steps of an algorithm that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. (Hint: see binary search algorithm.) Solution: procedure Search(x: integer; a 1 ,a 2 ,...,a n : integers) i := 1 j := n while (i<j - 2) (the list has at least four numbers) begin l := b j+3i 4 c (l is the last position of the first part, l = i + b j-i 4 c) m := b j+i 2 c (m is the last position of the second part, m = i + b 2(j-i) 4 c) u := b 3j+i 4 c (u is the last position of the third part, u = i + b 3(j-i) 4 c) if x>a u then i = u +1 (x is in the last part if x is on the list) else if x>a m then (x is in the third part if x is on the list) begin i := m +1 j := u end else if x>a l then (x is in the second part if x is on the list) begin i := l +1 j := m end else j := l (x is in the first part if x is on the list) end (Below checks if x is in the remaining list which has fewer than four elements.) k := b i+j 2 c (k is the position of the ”middle” number in the remaining list) if a i = x then location := i else if a k = x then location := k else if a j = x then location := j else location := 0 {location is the location of x or is 0 if x is not in the list.} 2. (a) Describe an algorithm that locates the last occurrence of the smallest element in a finite list of integers, where the integers in the list are not necessarily distinct. Solution: 1

Upload: kitty

Post on 15-Oct-2014

9.562 views

Category:

Documents


5 download

DESCRIPTION

Math 2320 York University Assignemnt 1 Solution

TRANSCRIPT

Math 2320 Assignment 1 Solution

1. Specify the steps of an algorithm that locates an element in a list ofincreasing integers by successively splitting the list into four sublists ofequal (or as close to equal as possible) size, and restricting the searchto the appropriate piece. (Hint: see binary search algorithm.)Solution:

procedure Search(x: integer; a1, a2, . . . , an : integers)i := 1j := n

while (i < j − 2) (the list has at least four numbers)begin

l := b j+3i

4 c (l is the last position of the first part, l = i + b j−i

4 c)

m := b j+i2 c (m is the last position of the second part, m = i + b 2(j−i)

4 c)

u := b 3j+i

4 c (u is the last position of the third part, u = i + b 3(j−i)4 c)

if x > au then i = u + 1 (x is in the last part if x is on the list)else if x > am then (x is in the third part if x is on the list)

begin

i := m + 1j := u

end

else if x > al then (x is in the second part if x is on the list)begin

i := l + 1j := m

end

else j := l (x is in the first part if x is on the list)end

(Below checks if x is in the remaining list which has fewer than four elements.)

k := b i+j2 c (k is the position of the ”middle” number in the remaining list)

if ai = x then location := i

else if ak = x then location := k

else if aj = x then location := j

else location := 0{location is the location of x or is 0 if x is not in the list.}

2. (a) Describe an algorithm that locates the last occurrence of thesmallest element in a finite list of integers, where the integersin the list are not necessarily distinct.Solution:

1

procedure last smallest(a1, a2, . . . , an : integers)min := a1

location := 1for i := 2 to n

begin

if ai ≤ min then

begin

min := ai

location := i

end

end

{location is the location of the last occurrence of the smallest element in the list.}

(b) Analyze the complexity of the algorithm you devised in Part (a),measured in terms of the number of comparisons.Solution:

For each i = 2, . . . , n, the for loop checks if i is larger than n andthe if statement performs one comparison. When i = n + 1, thefor statement sees that i > n and exits the loop. Hence if theinput is a list of n numbers, the algorithm uses 2(n−1)+1 = 2n−1comparisons. We say that this algorithm has complexity O(n).

3. The Horner’s method is an algorithm that evaluates polynomials.

The following pseudocode shows how to use this method to find thevalue of anxn + an−1x

n−1 + . . . + a1x + a0 at x = c.

procedure Horner(c, a0, a1, a2, . . . , an : real numbers)y := an

for i := 1 to n

y := y × c + an−i

end {y = ancn + an−1cn−1 + . . . + a1c + a0}

(a) Evaluate x2 + 5x + 3 at x = 2 by working through each step ofthe algorithm.Solution:

The inputs are c = 2, a0 = 3, a1 = 5 and a2 = 1. Note thatn = 2.At the first step, we set y := a2 = 1.For i = 1, we have

y := y × c + a2−1 = 1 × 2 + a1 = 1 × 2 + 5 = 7.

For i = 2, we have

y := y × c + a2−2 = 7 × 2 + a0 = 7 × 2 + 3 = 17.

2

We finish the for loop, and the output is 17 which is the value ofthe quadratic polynomial at x = 2.

(b) Exactly how many multiplications and additions are used by thisalgorithm to evaluate a polynomial of degree n at x = c?(Do not count additions used to increment the loop variable.)Solution:

In each iteration of the for loop, we computed

y := y × c + an−i

which consists of exactly one multiplication and one addition.Since there are n iterations in the for loop, so n multiplicationsand n additions are performed in this algorithm.

4. How many positive integers between 1000 and 9999 inclusive

(a) are even?Solution:

First note that there are (9999 − 1000 + 1) = 9000 numbers be-tween 1000 and 9999 inclusive. Exactly half of the numbers be-tween 1000 and 9999 are even, so there are 9000

2= 4500 even

numbers.

(b) have distinct digits?Solution:

The left-most digit is one of 1, 2, . . . , 9, so there are 9 possibleoutcomes.

The second left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the left-most digit, so there are 9 possible outcomes.

The third left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the two left-most digits, so there are 8 possible out-comes.

The fourth left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the three left-most digits, so there are 7 possible out-comes.

By the product rule, there are 9×9×8×7 integers between 1000and 9999 that have distinct digits.

(c) are not divisible by 5?Solution:

We observe that 200 × 5 = 1000 and 1999 × 5 = 9995 are the

3

smallest and the largest multiple of 5, respectively, between 1000and 9999 inclusive. Hence there are 1999 − 200 + 1 = 1800 mul-tiples of 5 between 1000 and 9999 inclusive. We conclude thatthere are 9000 − 1800 = 7200 numbers between 1000 and 9999inclusive that are not divisible by 5.

(d) are divisible by 5 and 7?Solution:

A number is divisible by 5 and 7 if and only if it is divisible by35.

We observe that 29 × 35 = 1015 and 285 × 35 = 9975 are thesmallest and the largest multiple of 35, respectively, between 1000and 9999 inclusive. Hence there are 285− 29 +1 = 257 multiplesof 35 between 1000 and 9999 inclusive.

(e) are divisible by 5 or 7?Solution:

From our solution of Part (c), there are 1800 numbers between1000 and 9999 inclusive that are divisible by 5.

We observe that 143×7 = 1001 and 1428×7 = 9996 are the small-est and the largest multiple of 7, respectively, between 1000 and9999 inclusive. Hence there are 1428 − 143 + 1 = 1286 multiplesof 7 between 1000 and 9999 inclusive.

From the previous part, there are 257 numbers between 1000 and9999 inclusive that are divisible by both 5 and 7.

By the principle of inclusion-exclusion, there are 1286 + 1800 −257 = 2829 numbers between 1000 and 9999 inclusive that aredivisible by 5 or 7.

(f) are not divisible by either 5 or 7?Solution:

From the previous part, there are 9000 − 2829 = 6171 numbersbetween 1000 and 9999 inclusive that are not divisible by either5 or 7.

(g) are divisible by 5 but not by 7?Solution:

Between 1000 and 9999 inclusive, there are 1800 numbers divisibleby 5. Among these 1800 numbers, 257 of them are also divisibleby 7. There are 1800 − 257 = 1543 numbers between 1000 and9999 inclusive that are divisible by 5 but not by 7.

5. How many strings of four decimal digits

4

(a) do not contain the same digit twice?Solution:

The left-most digit is one of 0, 1, . . . , 9, so there are 10 possibleoutcomes.

The second left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the left-most digit, so there are 9 possible outcomes.

The third left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the two left-most digits, so there are 8 possible out-comes.

The fourth left-most digit is one of 0, 1, . . . , 9 except for the onechosen for the three left-most digits, so there are 7 possible out-comes.

By the product rule, there are 10×9×8×7 strings of four decimaldigits not containing the same digit twice.

(b) end with an even digit?Solution:

Each of the three left-most digit is one of 0, 1, . . . , 9, so there are10 possible outcomes. The right-most digit is one of 0, 2, 4, 6, 8,so there are 5 possible outcomes.

By the product rule, there are 103 × 5 strings of four decimaldigits ending with an even digit.

(c) have exactly three digits that are 9’s?Solution:

Let A be the set of strings of four decimal digits having exacltythree digits that are 9’s.

For i = 1, . . . , 4, let Ai be the set of strings of four decimal digitswhose ith digit is not 9 and all the other three digits are 9’s. ThenA1, A2, A3 and A4 are pairwise disjoint and

A = A1 ∪ A2 ∪ A3 ∪ A4.

By the sum rule, we have

|A| = |A1| + |A2| + |A3| + |A4|.

For |Ai|, the ith digit is one of 0, 1, . . . , 8, so there are 9 possibleoutcomes. Since each of the other three digits is 9, Ai containsexactly 9 strings.

By the sum rule, |A| = 9 + 9 + 9 + 9 = 36.

5

(d) have at least one digit that is 9?Solution:

We first count the number of strings of four decimal digits is 104

since each digit is one of 0, 1, . . . , 9.

Now the number of strings of four decimal digits containing no9’s is 94 because each digit is one of 0, 1, . . . , 8.

The number of strings of four decimal digits having at least onedigit that is 9 is 104 − 94 = 3439.

(e) have one digit that is 9 and do not contain the same digit twice?Solution:

Let A be the set of strings of four decimal digits having one digitthat is 9 and not containing the same digit twice.

For i = 1, . . . , 4, let Ai be the set of strings of four decimal digitswhose ith digit is 9. Then A1, A2, A3 and A4 are pairwise disjointand

A = A1 ∪ A2 ∪ A3 ∪ A4.

By the sum rule, we have

|A| = |A1| + |A2| + |A3| + |A4|.

For |A1|, the first digit is 9. The second digit one of 0, 1, . . . , 8, sothere are 9 possible outcomes. The third digit is one of 0, 1, . . . , 8except for the one chosen for the second digit, so there are 8possible outcomes. The last digit is one of 0, 1, . . . , 8 except forthe ones chosen for the second and the third digits, so there are7 possible outcomes. By the product rule, A1 contains exactly1 × 9 × 8 × 7 strings.

Similarly, |A2| = |A3| = |A4| = 1 × 9 × 8 × 7.

By the sum rule, |A| = 4 × 1 × 9 × 8 × 7 = 2016.

6. (a) How many bit strings of length seven either begin with two 0’sor end with three 1’s?Solution:

Let A be the set of bit strings of length seven beginning with two0’s. Let B be the set of bit strings of length seven ending withthree 1’s.

For the strings in A, the first two digits are 0’s and each of the lastfive digits is either 0 or 1. Thus |A| = 1×1×2×2×2×2×2 = 32.

6

For the strings in B, the last three digits are 1’s and each of thefirst four digits is either 0 or 1. Thus |B| = 2×2×2×2×1×1×1 =16.

The strings in A∩B have 0’s in the first two digits and 1’s in thelast three digits. The third and the fourth digits of these stringsis either 0 or 1, so |A ∩ B| = 1 × 1 × 2 × 2 × 1 × 1 × 1 = 4.

By the principle of inclusion-exclusion, there are |A|+ |B| − |A∩B| = 32 + 16 − 4 = 44 bit strings of length seven either beginwith two 0’s or end with three 1’s.

(b) How many bit strings of length 10 contain either five consecutive0’s or five consecutive 1’s?Solution:

Let A be the set of bit strings of length 10 containing five con-secutive 0’s.

For i from 1 to 6, let Ai be the set of bit strings of length 10 inwhich the five consecutive 0’s starts at position i. Then A1, A2,A3, A4, A5 and A6 are pairwise disjoint and

A = A1 ∪ A2 ∪ A3 ∪ A4 ∪ A5 ∪ A6.

By the sum rule, we have

|A| = |A1| + |A2| + |A3| + |A4| + |A5| + |A6|.

For the strings in A1, the first five digits are 0’s and each of thelast five digits can be either 0 or 1. Hence

|A1| = 1 × 1 × 1 × 1 × 1 × 2 × 2 × 2 × 2 × 2 = 32.

For the strings in A2, the first digit is 1, the next five digits are0’s and each of the last four digits can be either 0 or 1. Hence

|A2| = 1 × 1 × 1 × 1 × 1 × 1 × 2 × 2 × 2 × 2 = 16.

For the strings in A3, the first digit is either 0 or 1, the seconddigit is 1, the next five digits are 0’s and each of the last threedigits can be either 0 or 1. Hence

|A3| = 2 × 1 × 1 × 1 × 1 × 1 × 1 × 2 × 2 × 2 = 16.

Similarly,|A4| = |A5| = |A6| = 16.

7

Consequently,

|A| = 32 + 16 + 16 + 16 + 16 + 16 = 112.

Let B be the set of bit strings of length 10 containing five conse-cuting 0’s. Similar to A, we have

|B| = 32 + 16 + 16 + 16 + 16 + 16 = 112.

NowA ∩ B = {0000011111, 1111100000}.

By the principle of inclusion-exclusion, the number of bit stringsof length 10 containing either five consecutive 0’s or five consec-utive 1’s is

|A ∪ B| = |A| + |B| − |A ∩ B| = 112 + 112 − 2 = 222.

8