This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: SECTION 2.4 Sequences and Summations (2)111 each case we just plug it = 8 into the formula.
‘1. nsﬂi 1no LA ’7 pl 1 {133 = 0 d) _(_2)8 = _«256 .6 a) The terms of this Se‘illlence alternate between 2 I ' ' ' I . 1f 13  . _ 
2‘I'O‘l'2+0+2+0+2+0+2=10. ( J n)and0_(lf9180dd) Thus the sum 18 b n n I i
) We can break this 1nto two parts and compute (2:0 33 ) — (2:20.23 ) . Each summation can be computed from the formula for the sum of a. geometric progression. Thus the answer is
39 — 1" 29 — 1.
3_1— 2_1 =9841—511=9330.l We will just write out the sums explicitly in each" case.
. a)(1—1)+(1—2)+(2—1)+(2—2)+(3—1)+(3—2)=3 — «
b) (0+0)+(0+2)+(0+4)+(3+0)+(3+2)+(3+4)+(6+0)+(6+2)+(6+4)+(9+0)+(9+2)+(9+4) = 78 c) (0+1+2)+(0+1+2)+(0+1+2)=
d) (0+0+0+0)+_(0+1+8+27)+(0+4+32+108)=1so ﬁllh. 11T__ *___
{NJL1: HJHE‘LWJﬂﬂrg‘— gin—Eur—T 1 .  1 1 e know from Example 21 that the set of real numbers between '0 and 1 is uncountable. Let us associate to
each real number in this range (including 0 but excluding 1) a function from the set‘ "bf‘posi'tW the set {0, 1, 2, 3,4, 5, 6, 7, 8, 9} as follows: If a: is a real number whose decimal representation is 0.d1d2d3 _. ..
(with ambiguity resolved by forbidding the decimal to end with an inﬁnite string of 9’s), then we associate to a: the function whose rule isrgiven by f (n) = d“. Clearly this is a onetojone function from the set of real numbers between 0 and 1 and a. subset of the set of all functions from the set of positive integers to the ,9}. Two different real numbers must have different decimal representations, so the
of forbidding representations such a contradiction. On the other hand, suppose that t $ T. Then because if i ' f (t), it follows that t i T; this is
E T ,this is again a contradiction. This completes our proof by contradiction 1 a I I r __1 r. I 1 Algorithms _— 55 ‘ 1 CHAPTER 3 — The Fundamentals: Algorithms, the Integers, and Matrices 3.1 Algorithms ' Elia) This procedure is not ﬁnite, since execution of the while 100p continues forever. This procedure is not effective. because the step m := 1/72, cannot be performed when n = 0. which will  '3'" eventually be the case. I‘J
..
‘l . :i. 'c) This procedure lacks deﬁniteness, since the value of l is never set. 'h
._;n'  .
=. ...J.'a'. .....[j'j #1.. . .. .. _ .. . ._ .. .. .. .. ... .. . .. r . .. . _.. .. _ ...... _ ... _... _ __ . ___.._.. ...... _.. _... __ n
"1
 " "'' "' " ".21.": :.' .. —:.'.'.:'_:=. ' '"_:...".'..:" 2.: ':''+‘ ==;—.:.......' .... ..'.'........ '_— "'2' 1: .' l' ' ' . ':' .::._'.'...: :...."— ;:::'..1..... .. ' — .._: .. .
_..—.... 1. .. I .  I ' 
.. ... __... ._ ..... _ ._ _ _... _. . _ . d) This procedure lacks deﬁniteness, since the statement does not tell whether a: is to be set equal to a or
to b. lIlIl'I—lI—I—Iu . _ ...; _____ I. 11...... .. .. .. .
—  .n . . . .  _._. .... 
' 11  "" . ' '4' . ' .. ..
... . .. __._.._. . .. ... ...._., . _ __
_ '.... ..__._.._ ... .. ...—u: r..=_ . ...
r' r...  Set the answer to be ——oo. For i going from 1 through 71 — 1, compute the value of the + 1)“ element in
the list minus the ith element in the list. If this is larger than the answer, reset the answer to be this value. procedure negatioes(a1. a2, . . . ,an : integers)
k := 0
for i := 1 to n. 
“Hal<0 then k:=k+1
end { k is the number of negative integers in the list} 8. This is similar to Exercise 7 , modiﬁed to keep track of the largest even integer we encounter. procedure largest even location(a1,a2. . . . .an : integers) largest := #00
for i := 1 to in.
if (a. is even and a. > largest) then
begin
k := '1',
largest := a9;
end
end It is the desired location (or 0 if there are no evens)} 10. We assume that if the input a" = 0, then n. _> 0. since otherwise ' 3:” is not deﬁned. In our procedure, we let
m = and compute mm in the obvious way. Then if n is negative, we replace the answer by its reciprocal. procedure powerﬁr : real number, n : integer)
m :=  . '
power := 1 I
for i := 1 to m
, power :2 power  a:
if n < 0 then power := 1 / power
{ power = 1r” } 12. Four assignment statements are needed. one for each of the variables and a temporary assignment to get
started so that we do not lose one of the original values. I Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices 56
temp = a:
:13 z: y
y := z
z := temp list.
b) We begin the search on the entire list, with the fourth element of the list. Since 7 > 5, we next
' = 5 and j = 8. This time we set m := 6 and compare 7 to next restrict ourselves to the ﬁrst half of the second half of the list, with i r: 5 and" j =
m := 5, and compare 7 to the ﬁfth element. Since 7 > 6, we now restrict ourselves to the portion of the list between i = 6 and j = 6. Since at this point t 5i 3', we exit the loop. Since the sixth element of the list is not equal to 7, we conclude that 7 is not in the list. i“: 1 and j = n = 8. We set m := 4 and compare 7 to
restrict the search to the second half of the list, with
the sixth element of the list. Since 7 )4 8, we if at, < min then min := a,
{ min is the smallest integer among the input} .l/s _ if 18. ighls 18 smnlar to Exerc1se 17.
f ! J
i
1 ,/ * procedure last smollest(a1,a2,. . . ,an : integers)
I I: (11
location := 1
for i. := 2 to n
if min _>_ a, then
begin
min 2": (If,
location := i ' end
‘ { location is the location of the last occurrence of the smallest element in the list} . , on, each of which is either a letter or a. blank.
A. l 1 22. We assume that the input is a sequence 0f Symb01Sl 31: 0'2: *  51" We build up the longest word in word ; its length is length. We denote the empty word by ii 60 Chapter 3 The Fundamentals: Algorithms, the Integers, and Matrices 46 We are counting just the comparisons of the numbers in the list, not any comparisonsneeded for the book ' keeping in the for loop. The second element in the list must be compared only with the ﬁrst (in other words, i“ when j = 2 in Algorithm 5, 2’ takes the values 1 before we drop out of the while loop). Similarly, the third
ﬁrst. We continue in this way, until ﬁnally the nth element must be element must be compared only with the  compared only with the ﬁrst. So the total number of comparisons is n — 1. This is the best case for insertion / . 48. For the inser the 8, one for the 1, four for the 5, and two for 13h tion sort, one comparison is needed to ﬁnd the correct location of the 4, one for the 3, four for
e 2. This is a total of 13 Comparisons. For the binary l i insertion sort, one comparison is needed to ﬁnd the correct location of the 4, two for the 3, two for the 8, three for the 1, three for the 5, and four for the 2. This is a total of 15 comparisons. If the list were long (and not almost in decreasing order to begin with), we would use many fewer comparisons using binary insertion
sort. The reason that the answer came out “wrong” here is that the list is so short that the binary search was not efficient. 50. a) This is essentially the same as Algorithm 5, but working from the other end. However, we can do the ' the searching for the correct insertion spot, so the pseudocode has only one section. procedmﬁrenliackivord insertion. so'r't(a1, a2, . . . ,an : real numbers with n 2 2) begin
m := a,
z = j — 1
while (m < a, and t > 0)
begin
ai+1 3: as.
2' '= i — 1
end
Gr+1 '= and found to be less, so the 3 moves to the right. We have reached the beginning of the list, so the loop terminates = O), and the 2 is inserted, yielding 2., 3, 4, 5, 1, 6. On the second pass the 4 is compared to the 3, and since 4 > 3, the while loop terminates and nothing
5 is inserted. One the fourth pass, the 1 is compared all the of the list as the comparisons go on,
ﬁnal pass produces no change. is immediately false. Therefore b) On the ﬁrst pass the 2 is compared to the 3 changes. Similarly, no changes are made as the
way to the front of the list, with each element moving toward the back and ﬁnally the 1 is inserted in its correct position, yielding 1, 2,I 3., 4, 5, 6. The
(3) Only one comparison is used during each pass, since the condition m < a, a total of n — l comparisons are used. I
j — 1 comparisons of elements, so the total number of comparisons is 1 + 2 + *   + 52. In each case we use as many quarters as we can, then as many dimes to achieve the remaining amount, then b) one quarter, leaving 24 cents, then two dimes, leaving 4 cents, then four pennies c) three quarters, leaving 24 cents, then two dimes, leaving .4 cents, than four pennies cl) one quarter, leaving 8 cents, then one nickel and three pennies ...
View
Full Document
 Spring '09

Click to edit the document details