{VERSION 5 0 "IBM INTEL NT" "5.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 1 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 256 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 262 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 263 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 265 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 266 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 267 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 268 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 270 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 271 "" 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 272 "" 0 1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 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 1" -1 3 1 {CSTYLE "" -1 -1 "Times " 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 1 0 1 0 2 2 0 1 } {PSTYLE "Normal" -1 256 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Title" -1 257 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }3 1 0 0 12 12 1 0 1 0 2 2 19 1 }{PSTYLE "Normal" -1 258 1 {CSTYLE "" -1 -1 "Times " 1 12 0 0 0 1 2 2 1 2 2 2 1 1 1 1 }1 1 0 0 0 0 1 0 1 0 2 2 0 1 }} {SECT 0 {EXCHG {PARA 257 "" 0 "" {TEXT -1 9 "Lecture 2" }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 15 "Shift + Return " }{TEXT 338 2 "vs" } {TEXT -1 11 " the Return" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT 260 66 "If the cursor is on the same line as a M aple command, hitting the " }{TEXT 262 6 "Return" }{TEXT 263 52 " key \+ will cause the command to be executed. Hitting " }{TEXT 334 14 "Shift \+ + Return" }{TEXT -1 16 " (hold down the " }{TEXT 335 5 "Shift" }{TEXT -1 17 " key and then hit" }{TEXT 336 7 " Return" }{TEXT -1 107 ") will move the cursor to the next line without executing the command and wi thout introducing a new prompt " }{TEXT 337 1 ">" }{TEXT -1 4 ". \n" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 261 209 "Sometimes you don't wa nt Maple to execute a sequence of commands till you have typed in seve ral on different lines. To do this one needs to know how to go to anot her output line without getting a new prompt, t" }{TEXT -1 25 "hat is, how to suppress " }{TEXT 256 5 ">. " }{TEXT 264 21 "In such cases \+ use of " }{TEXT 257 14 "Shift + Return" }{TEXT -1 16 " (hold down the \+ " }{TEXT 258 5 "Shift" }{TEXT -1 12 " key and hit" }{TEXT 259 7 " Retu rn" }{TEXT -1 24 ") will accomplish this. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 126 "Before continuing make sure yo u can type in the following in the space following without getting mor e that the initial prompt " }{TEXT 265 1 ">" }{TEXT -1 1 "." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 47 "for i from 1 to 3 do \n pri nt(\"Hello\");\nend do:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 35 "To get the indentation on the line " }{MPLTEXT 1 0 16 "print(\"Hello\"); " } {TEXT -1 62 "you will have to use the space bar to insert the extra sp aces." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 25 "for . . . do . . . end do" }}{EXCHG {PARA 0 "" 0 "" {TEXT 269 5 "Loop s" }{TEXT -1 4 " or " }{TEXT 270 8 "do loops" }{TEXT -1 203 " provide \+ a way to repeat operations a specified number of times. They are best \+ understood by examples. The example in the previous section showed a \+ way to print Hello three times. Execute the following:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "for i from 1 to 4 do \n i,i^2,i^3; \nend do;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "for x from -2 \+ to 2 do\n x, evalf(sin(x));\nend do;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 235 "Now we illustrate a common technique for adding a specif ied number of terms. There is nothing special about SUM as a variable. We could use any (unprotected) name for this variable. We will find t he sum of the integers 1 through 10. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 95 "We start by setting the variable SUM to 0. Examine the output carefully to see what's going on." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "SUM:=0;\n" }{TEXT -1 0 "" } {MPLTEXT 1 0 45 "for i from 1 to 5 do \n SUM:=SUM + i; \nend do;" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 53 "Note the value of i after the loop has been executed:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "i;" } }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 54 "See the value of SUM after the l oop has been executed:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "SU M;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 62 "To suppress the intermediat e steps we may place a colon after " }{TEXT 315 6 "end do" }{TEXT -1 35 " instead of a semi-colon and place " }{TEXT 317 4 "SUM;" }{TEXT -1 7 " after " }{TEXT 316 8 "end do: " }{TEXT -1 77 "This way we just \+ get the result desired with a minimum of unnecessary output." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 59 "SUM:=0:\nfor i from 1 to 5 d o \n SUM:= SUM + i; \nend do:\nSUM;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 31 "For another example we compute " }{XPPEDIT 18 0 "Sum(1/k,k = 1 \+ .. 100);" "6#-%$SumG6$*&\"\"\"F'%\"kG!\"\"/F(;F'\"$+\"" }{TEXT -1 32 " : This time we use the variable " }{TEXT 309 5 "total" }{TEXT -1 60 " \+ to keep track of the sum. Of course, any variable would do." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 61 "total:=0:\nfor k from 1 to 1 00 do \n total:=total+1/k; \nend do:" }{TEXT -1 0 "" }{MPLTEXT 1 0 1 " " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "total;" }{TEXT -1 0 "" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 49 "If we want to see how big this \+ is we apply evalf:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "evalf (total);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 26 "if . . . then . . . end if" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT 296 31 "Before givin g an example of an " }{TEXT 301 22 "if ... then ... end if" }{TEXT 302 26 " statement we discuss the " }{TEXT 304 3 "mod" }{TEXT 305 23 " procedure. The command" }{TEXT 300 9 " n mod d " }{TEXT 318 16 "or, e quivalently" }{TEXT 319 12 ", modp(n,d) " }{TEXT -1 67 "gives the rema inder when the integer n is divided by the integer d:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "12 mod 10 = modp(12,10);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "12 mod 2 = modp(12,2);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "12 mod 7 = modp(12,7);" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 13 "Note that if " }{TEXT 299 11 "n mo d d = 0" }{TEXT -1 13 " then d is a " }{TEXT 297 6 "factor" }{TEXT -1 4 " or " }{TEXT 298 7 "divisor" }{TEXT -1 38 " of n. Let's find all di visors of 28. " }{TEXT 339 62 "Notice the way we indent here, so it is easier to identify the" }{TEXT -1 1 " " }{MPLTEXT 1 0 0 "" }{TEXT 306 9 "do....end" }{TEXT 340 1 " " }{TEXT 307 2 "do" }{TEXT 341 6 " a nd " }{TEXT 308 13 "if ... end if" }{TEXT 342 15 " constructions." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 84 "n:=28;\nfor d from 1 to a do \n if n mod d = 0 then \n print(d); \n end if;\nend do:" }{TEXT -1 0 "" }{MPLTEXT 1 0 1 "\n" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 58 "Ch ange the value of n above and re-execute the procedure. " }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 33 "Here's another exa mple using the " }{TEXT 303 22 "if ... then ... end if" }{TEXT -1 47 " construction. Read it carefully. You should be" }}{PARA 0 "" 0 "" {TEXT -1 29 "able to see what's happening." }}}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 117 "for k from 1 to 50 do\n a:=3*k^2 + 10*k + 1:\n r := modp(a,31);\n if r < 3 then \n print(k,r); \n end if;\nend \+ do:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 55 "Note that the same task ca n be accomplished as follows:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 99 "for k from 1 to 50 do\n r:=modp(3*k^2 + 10*k + 1,31);\n if r \+ < 3 then print(k,r); end if;\nend do:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 42 "The Set of \+ Positive Divisors of an Integer" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 28 "Now we show how to form the " } {TEXT 266 3 "set" }{TEXT -1 113 " of all positive divisors of 28. A st udent of number theory will easily see how to improve this little prog ram. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 4 " The " }{TEXT 310 10 "printlevel" }{TEXT -1 75 " is 1 by default. Chang ing it to 2 allows us to see what's happening below:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "printlevel:=2:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "n:=28:\nS:=\{\}; " }{TEXT -1 49 "Start with the em pty set which is denoted by \{ \}." }{MPLTEXT 1 0 81 "\nfor d from 1 t o n do\n if n mod d = 0 then \n S:= S union \{d\}; \n end if;\nend d o;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 39 "Let's see what values d and S have now." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "d,S;\n" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 260 "N ow move the cursor back to printlevel:=2; , change the 2 to a 1 and re -execute all the above commands. Note that the intermediate steps are \+ now suppressed. Note that you may also change the assignment n:=28; to n:=1284; or anything you please and re-execute. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 61 "A simpler way to handle the problem in general is to build a " }{TEXT 311 9 "procedure" }{TEXT -1 89 " to find the set of all positiv e divisors of any integer n. We will call this procedure " }{TEXT 312 8 "Divisors" }{TEXT -1 120 ". Examine this procedure closely. Note the use of return on the next to last line of the definition of this \+ procedure. " }{TEXT 343 11 "DO NOT USE " }{TEXT 320 5 "print" }{TEXT 344 13 " in place of " }{TEXT 321 6 "return" }{TEXT 345 40 ". We will explain the difference later." }{TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 124 "Divisors:=proc(n)\nS:=\{\}:\nfor d from 1 to n \+ do\n if n mod d = 0 then \n S:=S union \{d\}; \n end if;\nend do;\nr eturn S;\nend proc;" }}}{EXCHG {PARA 258 "" 0 "" {TEXT -1 249 "Notice \+ the warning messages when you execute the statement defining the above procedure. Also notice that Maple reprints the procedure if you end t he definition with a semi-colon . We remedy both of these problems in \+ the following corrected version." }{TEXT 273 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 130 "Divisors:= proc(n)\nlocal S,d;\nS:=\{\}:\nfor d from 1 to n do\n if n mod d = 0 t hen S:=S union \{d\}; end if;\nend do;\nreturn S;\nend proc:\n" }}} {EXCHG {PARA 0 "" 0 "" {TEXT 271 10 "Important:" }{TEXT 313 190 " Make it a habit to end a procedure with a colon instead of a semi-colon to save output--especially when printing your worksheet. Also ALWAYS dec lare all variables used in a procedure to be " }{TEXT 322 5 "local" } {TEXT 323 61 " as we do above, unless there is a good reason not to do so. " }{TEXT -1 45 "Later we will discuss the difference between " } {TEXT 267 5 "local" }{TEXT -1 5 " and " }{TEXT 268 6 "global" }{TEXT -1 41 " variables.\n\nNow lets test the procedure " }{TEXT 272 8 "Divi sors" }{TEXT -1 186 ": Try some things first that we know the answer t o in order to make some check on the correctness. Of course, this does n't prove it works always but often such checks will catch errors. " } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "Divisors(1);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "Divisors(2);" }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 12 "Divisors(6);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "Divisors(126);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "Divisors(12345);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 " " }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 28 "More Examples of Procedures " }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 49 "Let's look again at how to co mpute a sum such as " }{XPPEDIT 18 0 "Sum(i^2,i = 1 .. 4);" "6#-%$SumG 6$*$%\"iG\"\"#/F';\"\"\"\"\"%" }{TEXT -1 95 ". Let's try to make it ge neral by introducing a variable n. We start by setting it equal to 4: " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 72 "n:=4:\nTotal:=0:\nfor i from 1 to n do\n Total:=Total+i^2:\nend do:\nTotal;\n" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 152 "We could simply change the assignment of n:=4 to n:=10 or any other value and re-execute to find other sums. I nstead, let's make a procedure to compute " }{XPPEDIT 18 0 "Sum(i^2,i \+ = 1 .. n);" "6#-%$SumG6$*$%\"iG\"\"#/F';\"\"\"%\"nG" }{TEXT -1 65 " fo r any given positive integer n. We will call this procedure F:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 121 "F:=proc(n)\n local i,Total ;\n Total:=0:\n for i from 1 to n do\n Total:=Total+i^2:\n end d o;\n return Total;\nend proc:\n" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 36 "We apply this to n = 4 and n = 1000:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "F(4);\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "F (1000);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 42 "Now we make a procedur e that will compute " }{XPPEDIT 18 0 "sum(f(i),i = a .. b);" "6#-%$sum G6$-%\"fG6#%\"iG/F);%\"aG%\"bG" }{TEXT -1 113 " where a and b are any \+ integers and f is any function. We will call the procedure S. Note tha t it requires three " }{TEXT 346 9 "arguments" }{TEXT -1 13 ": f, a an d b:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 117 "S:=proc(f,a,b)\nlo cal i, Total;\nTotal:=0;\nfor i from a to b do\n Total:=Total + f(i); \nend do;\nreturn Total;\nend proc:\n" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 37 "Some applications of our procedure S:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "S(sin,1,5);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "S(x->x^2, 1, 6);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "S(f,-3,5);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" } }{PARA 0 "" 0 "" {TEXT -1 70 "Next we make a procedure we call H which has the property that H(n) = " }{XPPEDIT 18 0 "infinity;" "6#%)infini tyG" }{TEXT -1 48 " if n is greater than 100 and H(n) = 0 if not." } }{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 66 "H:=proc(n)\nif n > 100 then \n return infinity; \nfi;\nreturn 0 ;\nend:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 66 "Note that if n > 100 is true then H immediately returns t he value " }{XPPEDIT 18 0 "infinity" "6#%)infinityG" }{TEXT -1 146 " a nd therefore does not proceed to the next statement. On the other hand , if n > 100 is false then Maple skips down to the last line a returns 0. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "H(221);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "H (11);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 34 "Making a Program into a Procedure." }} {PARA 0 "" 0 "" {TEXT -1 134 "When constructing a procedure it is a go od idea to test the commands individually first so you can be sure wha t is happening. One may " }{TEXT 324 3 "cut" }{TEXT -1 2 ", " }{TEXT 325 4 "copy" }{TEXT -1 5 " and " }{TEXT 326 5 "paste" }{TEXT -1 187 " \+ text and Maple input and output pretty much at will. This is useful w hen constructing a procedure from some program that you have already t ested. You may illustrate this by copying the " }{TEXT 350 7 "program " }{TEXT -1 28 " below and making it into a " }{TEXT 351 9 "procedure " }{TEXT -1 19 " as we did above. " }{TEXT 352 204 "To perform calcul ations with Maple one need not always write a procedure. Writing a pr ocedure is really only necessary if you need to use some program repea tedly with different values of the parameters. " }}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 71 "n:=4:\nTotal:=0:\nfor i from 1 to n do\n Tota l:=Total+i^2:\nend do:\nTotal;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 39 "Order of Execution of Commands in Maple" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 347 "In a single execution group Maple executes the commands in order from top to bottom. However, if you go back and change some computation in a different execution group then it does not change the following res ults unless the entire worksheet is executed once more. Here is a simp le example. First execute the following commands in the order given." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=2;" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 5 "y:=3;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "z:=x*y;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 190 "Now go back and change the 2 in t he line x:=2 to a 5 and execute that command. Notice that the value of z has not changed. To change the value of z to 15 we must re-execute the line z:=x*y." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 227 "Of course, we would not have this problem if we put all the three commands in a single execution group \+ as we do below. But after changing the 2 to a 5 in x:=2; we must execu te the execution group for the changes to take effect." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "x:=2:\ny:=3:\nz:=x*y:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}}{EXCHG {PARA 258 "" 0 "" {TEXT -1 176 "The moral of this is that when executing the lines in a \+ Maple worksheet it is important which order they are executed in rathe r than the order in which they appear on the page." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 40 "A ssignment 2 (Due Tuesday, September 10)" }}{EXCHG {PARA 0 "" 0 "" {TEXT 288 132 "From now on 25% will be deducted from each assignment \+ which does not conform to the format described in Assignment 1 Problem 1 (a)." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 347 130 "Points will be deleted for any procedure that uses local variable s that are not declared to be local in the body of the procedure." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 348 82 "Points w ill be deleted for any procedure definition that does not end in a col on. " }{TEXT 280 12 "\n\nProblem 1." }{TEXT -1 61 " \n(a) Write a pro gram (not necessarily a procedure) using a " }{TEXT 314 7 "do loop" } {TEXT -1 20 " to compute the sum " }{XPPEDIT 18 0 "sum((2/3)^n,n = 2 . . 100);" "6#-%$sumG6$)*&\"\"#\"\"\"\"\"$!\"\"%\"nG/F,;F(\"$+\"" } {TEXT -1 9 ". Apply " }{TEXT 281 5 "evalf" }{TEXT -1 54 " to obtain a floating point approximation to the sum.\n" }{MPLTEXT 1 0 0 "" } {TEXT -1 70 "\n(b) Write a program (not necessarily a procedure) that \+ will find the " }{TEXT 282 3 "set" }{TEXT -1 107 " S of all positive i ntegers less than 1000 that have 11 as a factor. [n has 11 as a facto r if and only if " }{XPPEDIT 18 0 "`mod`(n,11) = 0;" "6#/-%$modG6$%\"n G\"#6\"\"!" }{TEXT -1 2 ".]" }}{PARA 0 "" 0 "" {TEXT 277 12 "\nProblem 2. " }{TEXT -1 20 "\n\nWrite a procedure " }{TEXT 289 7 "ISPRIME" } {TEXT -1 57 " whose input is a positive integer n and whose output is \+ " }{TEXT 294 4 "true" }{TEXT -1 19 " if n is prime and " }{TEXT 295 5 "false" }{TEXT -1 58 " otherwise. Test the procedure on the following numbers: " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 45 "1, 2, 3, 4, 11, 111, 531, 7919, 15689, 262681" }}{PARA 0 "" 0 " " {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 21 "The answers should be" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {XPPEDIT 18 0 "false truetruefalsetruefalsefalsetruefalsetrue;" "6#%Nfalsetruetruefalsetrue falsefalsetruefalsetrueG" }{TEXT 284 8 " \n\nHint:" }{TEXT -1 36 " Her e's a start for the program:\n\n> " }{MPLTEXT 1 0 111 " ISPRIME:=proc( n)\n local d;\n if n < 2 then return false; end if;\n if n = 2 t hen return true; end if;\n \n" }{TEXT -1 47 "Note that if n < 2 then the third line causes " }{TEXT 290 10 "ISPRIME(n)" }{TEXT -1 55 " to return false. If n = 2 then the fourth line causes " }{TEXT 291 7 "IS PRIME" }{TEXT -1 424 " to return true. If neither is true then n is at least 3. So now you only need to handle in the rest of the program t he case that n is at least 3. This can be done by using a loop to chec k whether or not n is divisible by an integer d from 2 to n-1. If you \+ find such a value of d you want to return false. If the loop is comple ted without finding any such d then you should return true. You may be able to improve on this.\n" }}{PARA 0 "" 0 "" {TEXT 283 6 "Remark" } {TEXT -1 48 ". Maple actually has a built-in procedure named " }{TEXT 287 7 "isprime" }{TEXT -1 47 " which performs the same task as the pro cedure " }{TEXT 292 7 "ISPRIME" }{TEXT -1 16 " in Problem 2. " }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 18 "Maple's p rocedure " }{TEXT 330 7 "isprime" }{TEXT -1 13 " is really a " }{TEXT 332 31 "probabilistic primality tester." }{TEXT -1 5 " See " }{TEXT 331 8 "?isprime" }{TEXT -1 28 ". To see how the procedure " }{TEXT 329 7 "isprime" }{TEXT -1 36 " is constructed execute the command " } {TEXT 328 19 "showstat(isprime); " }{TEXT -1 145 "However, it is not e asy to see what the program is doing. So I don't recommend spending a \+ lot of time on trying to figure it out unless you have " }{TEXT 349 4 "lots" }{TEXT -1 48 " of experience in programming and number theory. " }}{PARA 0 "" 0 "" {TEXT -1 1 "\n" }{TEXT 278 10 "Problem 3." }{TEXT -1 60 " \n\nIf n is a positive integer greater than 2 that satisfies \+ " }{XPPEDIT 18 0 "modp(2^(n-1),n) = 1;" "6#/-%%modpG6$)\"\"#,&%\"nG\" \"\"F+!\"\"F*F+" }{TEXT -1 39 " and n is not prime then n is called a \+ " }{TEXT 327 13 "2-pseudoprime" }{TEXT -1 95 ". Find all 2-pseudoprime s less than 2000 and give the factorization of each one that you find. " }{TEXT 333 4 "Hint" }{TEXT -1 44 ": The smallest 2-pseudoprime is 3 41 = 11x31." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 1 " " }}{PARA 0 "" 0 "" {TEXT 279 11 "\nProblem 4." }{TEXT -1 27 " \+ \n\n(a) Write a procedure, " }{TEXT 274 8 "primesum" }{TEXT -1 92 ", \+ whose input is a positive integer n and whose output is the sum of all primes p such that " }{XPPEDIT 18 0 "p <= n;" "6#1%\"pG%\"nG" }{TEXT -1 37 ". You may use either your procedure " }{TEXT 293 7 "ISPRIME" } {TEXT -1 42 " from problem 2 or the built-in procedure " }{TEXT 275 9 "isprime. " }{TEXT -1 46 "Test the procedure for n = 2, 4, 10, and 100 0." }}{PARA 0 "" 0 "" {TEXT -1 25 "\n (b) Write a procedure, " }{TEXT 285 8 "primeset" }{TEXT -1 93 ", whose input is a positive integer n \+ and whose output is the set of all primes p such that " }{XPPEDIT 18 0 "p <= n;" "6#1%\"pG%\"nG" }{TEXT -1 87 ". You may use either your pr ocedure ISPRIME from problem 2 or the built-in procedure " }{TEXT 286 9 "isprime. " }{TEXT -1 46 "Test the procedure for n = 2, 4, 10, a nd 1000." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 276 1 " " }}}{PARA 3 "" 0 "" {TEXT -1 0 "" }}}}{MARK "8 0 0" 39 } {VIEWOPTS 0 0 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }