{VERSION 5 0 "APPLE_PPC_MAC" "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 1 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 1 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 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 1 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 1 } {CSTYLE "" -1 266 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 267 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 268 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 270 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 271 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 272 "" 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0 0 0 0 0 }{CSTYLE "" -1 334 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 335 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 336 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 337 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 338 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 339 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 340 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 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 40 "Shift + Return key versus the Return key " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT 266 78 "If the cursor is on the same line as a well formed Maple command, \+ hitting the " }{TEXT 268 6 "Return" }{TEXT 269 44 " key will cause the command to be executed. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT 267 246 "Sometimes commands are several lines long and yo u don't want Maple to execute the commands or sequence of commands til l you have typed in all the lines. To do this one needs to know how to go to another output line without getting a new prompt, t" }{TEXT -1 25 "hat is, how to suppress " }{TEXT 256 5 ">. " }{TEXT 270 21 "In \+ such cases use of " }{TEXT 257 14 "Shift + Return" }{TEXT -1 16 " (hol d down the " }{TEXT 258 5 "Shift" }{TEXT -1 17 " key and then hit" } {TEXT 259 7 " Return" }{TEXT -1 63 ") will accomplish this. \n\nIf the above doesn't work look under " }{TEXT 261 4 "Edit" }{TEXT -1 16 " an d go down to " }{TEXT 260 13 "Preferences. " }{TEXT -1 10 "Make sure \+ " }{TEXT 262 29 "Execute Input with Return Key" }{TEXT -1 24 " is chec ked. Otherwise " }{TEXT 264 6 "Return" }{TEXT -1 74 " will move one t o the next line and execution will occur only with the " }{TEXT 263 5 "Enter" }{TEXT -1 5 " key." }{TEXT 265 0 "" }{TEXT -1 105 "\n\nB efore continuing make sure you can type in the following without getti ng more that the initial prompt " }{TEXT 271 1 ">" }{TEXT -1 1 "." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 48 "for i from 1 to 3 do \n pri nt(\"Hello\");\nend do;\n" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 27 "for . . . do . . . end d o" }}{EXCHG {PARA 0 "" 0 "" {TEXT 276 5 "Loops" }{TEXT -1 4 " or " } {TEXT 277 8 "do loops" }{TEXT -1 126 " provide a way to repeat operati ons a specified number of times. They are best understood by examples. Execute the following:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 42 "for i from 1 to 5 do \n i,i ^2,i^3;\nend do;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 278 "Now we illus trate a common technique for adding a specified number of terms. There is nothing special about SUM as a variable. We could use any name for this variable. We will find the sum of the integers 1 through 10. \+ (Note that we start by setting the variable SUM to 0.) " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "SUM:=0;\n" }{TEXT -1 0 "" }{MPLTEXT 1 0 46 "for i from 1 to 10 do \n SUM:=SUM + i; \nend do;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 53 "Note the value of i after the loop has be en executed:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "i;" }}} {EXCHG {PARA 0 "" 0 "" {TEXT -1 50 "The value of SUM after the loop ha s been executed:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "SUM;" }} }{EXCHG {PARA 0 "" 0 "" {TEXT -1 65 "To suppress the intermediate step s we may do place a colon after " }{TEXT 322 6 "end do" }{TEXT -1 35 " instead of a semi-colon and place " }{TEXT 324 4 "SUM;" }{TEXT -1 7 " after " }{TEXT 323 8 "end do: " }{TEXT -1 40 "This way we just get th e result desired." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 60 "SUM:=0 :\nfor i from 1 to 10 do \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 316 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 100 do \n total:=total+1/k; \nend do:" } {TEXT -1 0 "" }{MPLTEXT 1 0 1 " " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "total;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "e valf(total);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "?modp" }}}} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 26 "if . . . then . . . end if" }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT 303 31 "B efore giving an example of an " }{TEXT 308 22 "if ... then ... end if " }{TEXT 309 26 " statement we discuss the " }{TEXT 311 3 "mod" } {TEXT 312 23 " procedure. The command" }{TEXT 307 9 " n mod d " } {TEXT 325 15 "or equivalently" }{TEXT 326 11 " modp(n,d) " }{TEXT -1 67 "gives the remainder when the integer n is divided by the integer d :" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 23 "12 mod 10, modp(12,10) ;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "12 mod 2, modp(12,2); " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "12 mod 7, modp(12,7);" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 13 "Note that if " }{TEXT 306 11 "n mod d = 0" }{TEXT -1 13 " then d is a " }{TEXT 304 6 "factor" }{TEXT -1 4 " or " }{TEXT 305 7 "divisor" }{TEXT -1 102 " of n. Let's find al l divisors of 126; Notice the way we indent here, so it is easier to i dentify the " }{MPLTEXT 1 0 0 "" }{TEXT 313 9 "do....end" }{TEXT -1 1 " " }{TEXT 314 2 "do" }{TEXT -1 6 " and " }{TEXT 315 13 "if ... end i f" }{TEXT -1 15 " constructions." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 85 "n:=126;\nfor d from 1 to n 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 "Change the value of n abov e and re-execute the procedure. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 33 "Here's another example using the " } {TEXT 310 22 "if ... then ... end if" }{TEXT -1 14 " construction." }} }{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 53 "No te that the above could 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 273 3 "set" }{TEXT -1 118 " of all positive \+ divisors of 126.\nA student of number theory will easily see how to im prove this little program. The " }{TEXT 317 10 "printlevel" }{TEXT -1 75 " is 1 by default. Changing it to 2 allows us to see what's happ ening below:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "printlevel: =2:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "n:=126:\nS:=\{\}; " }{TEXT -1 49 "Start with the empty set which is denoted by \{ \}." } {MPLTEXT 1 0 83 "\nfor d from 1 to n do\n if n mod d = 0 then \n S:= S union \{d\}; \n end if;\nend do;\n\n" }}}{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 9 "Note the " }{TEXT 272 22 "if .. then ... end if;" }{TEXT -1 15 " construction. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 261 "Now move the cursor back to printlevel:=2; , change the \+ 2 to a 1 and re-execute all the above commands. Note that the intermed iate steps are now suppressed. Note that you may also change the assig nment n:=126; 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 318 9 "procedure" }{TEXT -1 89 " to find the se t of all positive divisors of any integer n. We will call this proced ure " }{TEXT 319 8 "Divisors" }{TEXT -1 131 ". Examine this procedure \+ closely. Note the use of return on the next to last line of the defini tion of this procedure. DO NOT USE " }{TEXT 327 5 "print" }{TEXT -1 13 " in place of " }{TEXT 328 6 "return" }{TEXT -1 40 ". We will expl ain the difference later." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 125 "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;\nreturn S;\nend proc;\n" }}}{EXCHG {PARA 258 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 258 "" 0 "" {TEXT -1 239 "Notice the warning messages when you execute the statement defini ng the above procedure. Also notice that Maple reprints the procedure \+ if you end the definition with ; . We remedy both of these problems i n the following corrected version." }{TEXT 280 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 278 10 "Important:" }{TEXT 320 174 " Make it a habit to end a procedure with : instead of ; to save output--es pecially when printing your worksheet. Also ALWAYS declare all variabl es used in a procedure to be " }{TEXT 329 5 "local" }{TEXT 330 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 274 5 "local " }{TEXT -1 5 " and " }{TEXT 275 6 "global" }{TEXT -1 41 " variables. \n\nNow lets test the procedure " }{TEXT 279 8 "Divisors" }{TEXT -1 50 ": Try somethings first that we know the answer to:" }}}{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 "Div isors(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 compute a sum such as " }{XPPEDIT 18 0 "Sum(i^2,i = 1 .. 4);" "6#-%$SumG6$*$% \"iG\"\"#/F';\"\"\"\"\"%" }{TEXT -1 85 ". Let's try to make it general by introducing a variable n and 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. Instead, let's \+ make a procedure to compute " }{XPPEDIT 18 0 "Sum(i^2,i = 1 .. n);" "6 #-%$SumG6$*$%\"iG\"\"#/F';\"\"\"%\"nG" }{TEXT -1 65 " for any given po sitive 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 do;\n return Tota l;\nend proc:\n" }}}{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 38 "Here's a procedure that will compute " } {XPPEDIT 18 0 "sum(f(i),i = a .. b);" "6#-%$sumG6$-%\"fG6#%\"iG/F);%\" aG%\"bG" }{TEXT -1 135 " where a and b are any integers and f is any f unction. We will call the procedure S. Note that it requires three arg uments: f, a and b:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 117 "S:= proc(f,a,b)\nlocal 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 119 "Here is a procedure name d H which has the property that H(n) = big if n is greater than 100 a nd H(n) = little if not." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 66 "H:=proc(n)\nif n > 100 then \n ret urn big; \nfi;\nreturn little;\nend:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 220 "Note that if n > 100 is true t hen H immediately returns the value big and therefore does not proceed to the next statement. On the other hand, if n > 100 is false then Ma ple skips down to the last line a returns little. " }}{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 19 "Cutt ing and Pasting" }}{PARA 0 "" 0 "" {TEXT -1 134 "When constructing a p rocedure it is a good idea to test the commands individually first so \+ you can be sure what is happening. One may " }{TEXT 331 3 "cut" } {TEXT -1 2 ", " }{TEXT 332 4 "copy" }{TEXT -1 5 " and " }{TEXT 333 5 " paste" }{TEXT -1 250 " text and Maple input and output pretty much at \+ will. This is useful when constructing a procedure from some program \+ that you have already tested. You may illustrate this by copying the p rogram below and making it into a procedure as we did above. " }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 71 "n:=4:\nTotal:=0:\nfor i from 1 to n do\n Total:=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 Comma nds in Maple" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 " " {TEXT -1 347 "In a single execution group Maple executes the command s 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 th e following results unless the entire worksheet is executed once more. Here is a simple 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 cha nge the 2 in the line x:=2 to a 5 and execute that command. Notice tha t the value of z has not changed. To change the value of z to 15 we m ust re-execute the line z:=x*y." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 224 "Of course, we would not have this problem if we put all the three commands in a single ex ecution groups as follows: But after changing the 2 to a 5 in x:=2; we must execute 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 ra ther 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 "Assignment 2 (Due Wednesday, January 23)" }}{EXCHG {PARA 0 "" 0 "" {TEXT 295 334 "From now on: 25% will be deducted from each assig nment which does not conform to the format described in Assignment 1 P roblem 1 (a). Also some points will be deleted for any procedure that use local variables that are not declared to be local in the body of \+ the procedure and any procedure definition that does not end in a col on. " }{TEXT 287 12 "\n\nProblem 1." }{TEXT -1 31 " \n(a) Write a pro gram using a " }{TEXT 321 7 "do loop" }{TEXT -1 20 " to compute the su m " }{XPPEDIT 18 0 "sum((2/3)^n,n = 2 .. 100);" "6#-%$sumG6$)*&\"\"#\" \"\"\"\"$!\"\"%\"nG/F,;F(\"$+\"" }{TEXT -1 9 ". Apply " }{TEXT 288 5 "evalf" }{TEXT -1 94 " to obtain a floating point approximation to the sum.\n\n(b) Write a program that will find the " }{TEXT 289 3 "set" } {TEXT -1 107 " S of all positive integers less than 1000 that have 11 \+ as a factor. [n has 11 as a factor if and only if " }{XPPEDIT 18 0 "` mod`(n,11) = 0;" "6#/-%$modG6$%\"nG\"#6\"\"!" }{TEXT -1 2 ".]" }} {PARA 0 "" 0 "" {TEXT 284 12 "\nProblem 2. " }{TEXT -1 20 "\n\nWrite a procedure " }{TEXT 296 7 "ISPRIME" }{TEXT -1 57 " whose input is a po sitive integer n and whose output is " }{TEXT 301 4 "true" }{TEXT -1 19 " if n is prime and " }{TEXT 302 5 "false" }{TEXT -1 58 " otherwise . Test the procedure on the following numbers: " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 29 "1, 2, 3, 4, 111, 531, 79 19, " }{XPPEDIT 18 0 "15689;" "6#\"&*o:" }{TEXT -1 15 ", and 262681. \+ " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 21 "The a nswers should be" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 1 " " }{XPPEDIT 18 0 "false,true,true,false,false,false,true, false,true;" "6+%&falseG%%trueGF$F#F#F#F$F#F$" }{TEXT 291 9 ". \n\nHin t:" }{TEXT -1 36 " Here'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 then return true; end if;\n \n" }{TEXT -1 47 "N ote that if n < 2 then the third line causes " }{TEXT 297 10 "ISPRIME (n)" }{TEXT -1 55 " to return false. If n = 2 then the fourth line cau ses " }{TEXT 298 7 "ISPRIME" }{TEXT -1 424 " to return true. If neithe r is true then n is at least 3. So now you only need to handle in the rest of the program the case that n is at least 3. This can be done b y using a loop to check 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 completed without finding any such d then you should r eturn true. You may be able to improve on this.\n" }}{PARA 0 "" 0 "" {TEXT 290 6 "Remark" }{TEXT -1 48 ". Maple actually has a built-in pro cedure named " }{TEXT 294 7 "isprime" }{TEXT -1 47 " which performs th e same task as the procedure " }{TEXT 299 7 "ISPRIME" }{TEXT -1 16 " i n Problem 2. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 18 "Maple's procedure " }{TEXT 337 7 "isprime" }{TEXT -1 13 " is really a " }{TEXT 339 31 "probabilistic primality tester." }{TEXT -1 5 " See " }{TEXT 338 8 "?isprime" }{TEXT -1 28 ". To see how the p rocedure " }{TEXT 336 7 "isprime" }{TEXT -1 36 " is constructed execut e the command " }{TEXT 335 19 "showstat(isprime); " }{TEXT -1 197 "How ever, it is not easy to see what the program is doing. So I don't reco mmend spending a lot of time on trying to figure it out unless you hav e lots of experience in programming and number theory." }}{PARA 0 "" 0 "" {TEXT -1 1 "\n" }{TEXT 285 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 334 13 "2-pseudoprime" }{TEXT -1 95 ". Find all 2-pseudoprimes less than 2 000 and give the factorization of each one that you find. " }{TEXT 340 4 "Hint" }{TEXT -1 44 ": The smallest 2-pseudoprime is 341 = 11x31 ." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 1 " " }} {PARA 0 "" 0 "" {TEXT 286 11 "\nProblem 4." }{TEXT -1 27 " \n\n(a) Wr ite a procedure, " }{TEXT 281 8 "primesum" }{TEXT -1 92 ", whose input is a positive integer n and whose output is the sum of all primes p s uch that " }{XPPEDIT 18 0 "p <= n;" "6#1%\"pG%\"nG" }{TEXT -1 37 ". Yo u may use either your procedure " }{TEXT 300 7 "ISPRIME" }{TEXT -1 42 " from problem 2 or the built-in procedure " }{TEXT 282 9 "isprime. " }{TEXT -1 46 "Test the procedure for n = 2, 4, 10, and 1000." }} {PARA 0 "" 0 "" {TEXT -1 25 "\n (b) Write a procedure, " }{TEXT 292 8 "primeset" }{TEXT -1 93 ", whose input is a positive integer n and wh ose 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 procedure ISPRIME from problem 2 or the built-in procedure " }{TEXT 293 9 "is prime. " }{TEXT -1 46 "Test the procedure for n = 2, 4, 10, and 1000. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT 283 1 " " }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 3 "" 0 "" {TEXT -1 0 "" } }}}{MARK "9" 0 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }