(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 8097, 261]*) (*NotebookOutlinePosition[ 9128, 294]*) (* CellTagsIndexPosition[ 9084, 290]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData["13. Reprise: Syntax and Data Types: Homework"], "Title", Evaluatable->False, AspectRatioFixed->True], Cell["Last revision: February 18 1998", "SmallText", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell[TextData["Author: YourNameGoesHere"], "Subtitle", CellFrame->True, Evaluatable->False, AspectRatioFixed->True, Background->GrayLevel[0.899992]], Cell[CellGroupData[{ Cell[TextData["Precedence and Associativity"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "Show the complete order of evaluation (by placing parentheses everywhere \ necessary) of the following expression:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["a = b = c = 2 - 2- 2 ^ 2 ^ 2"], "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Machine-Precision Integers"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "Determine the range of possible machine-precision integers (both positive \ and negative) on a computer which devotes the following number of bits to \ integers. (Expand your answer out, if necessary, to an integer; instead of \ answering \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["2^3", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", for instance, answer ", Evaluatable->False, AspectRatioFixed->True], StyleBox["8", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[".)\n\n", Evaluatable->False, AspectRatioFixed->True], StyleBox["(a) 32\n(b) 16\n(c) 8", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Machine-Precision Approximations"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Recall that function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["N[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" forces the argument into a machine-precision approximation with a fixed \ number of digits. Show that the numbers ", Evaluatable->False, AspectRatioFixed->True], StyleBox["1.2345678901234567890123", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" and ", Evaluatable->False, AspectRatioFixed->True], StyleBox["1.2345678901234567890124", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " are indistinguishable when converted to machine-precision. Hint: convert \ each one, and then subtract.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Reading Code"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "Up to now we have often been writing code, not reading it, and the various \ pronouncements on style might easily go unappreciated. Here we present \ several sections of code and ask you to determine what exactly the section of \ code performs. One reasonable method to go about \"deciphering\" code is to \ read it over once, check some input and output, and then delve into one \ function at a time. If the functions have usage lines, all the better; if \ not, you will have to consider how they work solely from the function \ definitions inside. As you work through function by function, consider if \ any variables are local or global, if modules have side effects, and be \ careful unravelling anonymous functions! Make sure to comment on style, \ local and global variables, readability, comments, and usage lines."], "Text",\ Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["(a) ", Evaluatable->False, AspectRatioFixed->True], StyleBox["split", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "Clear[split]\nsplit[a_,b_] := Module[\n\t\t {low=If[a>b,b,a], \ high=If[a>b,a,b],flip=0},\n\t\t makemid[x_,y_] := N[(x+y)/2];\n\t\t \ absval[x_] := If[x>0,x,-x];\n\t\t While[absval[high-low]>0.001,\n\t\t\t\t \ If[flip==0, (* check flip *)\n\t\t\t\t (high=makemid[low,high]; flip=1),\n\t\ \t\t\t (low=makemid[low,high]; flip=0)\n\t\t\t\t ]\n\t\t ];\n\t\t \ {low,high} (* output low and high *)\n\t\t]\n\t\t\t\t\t\t"], "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["(b) ", Evaluatable->False, AspectRatioFixed->True], StyleBox["everyOtherPrime", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "Clear[everyOtherPrime]\neveryOtherPrime[n_Integer] := \n Module[{facs},\n \ facs=FactorInteger[n];\n facs=Map[(First[#])&,Partition[facs,2]];\n \ facs\n ]\neveryOtherPrime::usage=\"everyOtherPrime[n] decomposes an Integer \ n into its prime factors and returns a list of pairs {p1,p2} where p1 is a \ prime and p2 is the power of the prime in the Integer n. Note that \ everyOtherPrime skips every other prime in the decomposition.\";\n\t "], "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["(c) ", Evaluatable->False, AspectRatioFixed->True], StyleBox["myFun", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "Clear[myFun]\nmyFun[s_String?((StringLength[#]>5)&),a_Integer:1] :=\n\t \ Module[ {wonderful,amazing},\n\t \ wonderful=Map[ToCharacterCode,Characters[s]];\n\t \ amazing=Map[Last,Partition[Sort[wonderful],a]];\n\t \ incredible=Join[Map[FromCharacterCode,amazing]];\n\t incredible\n\t ];"], "Input", AspectRatioFixed->True] }, Closed]] }, Open ]] }, Open ]] }, FrontEndVersion->"Macintosh 3.0", ScreenRectangle->{{0, 832}, {0, 604}}, WindowToolbars->{}, WindowSize->{520, 509}, WindowMargins->{{29, Automatic}, {Automatic, 31}}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False}, MacintoshSystemPageSetup->"\<\ 00<0004/0B`000002n88o?mooh<" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 121, 2, 136, "Title", Evaluatable->False], Cell[1855, 55, 101, 2, 26, "SmallText", Evaluatable->False], Cell[CellGroupData[{ Cell[1981, 61, 156, 4, 78, "Subtitle", Evaluatable->False], Cell[CellGroupData[{ Cell[2162, 69, 108, 2, 46, "Subsection", Evaluatable->False], Cell[2273, 73, 189, 4, 46, "Text", Evaluatable->False], Cell[2465, 79, 81, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[2583, 85, 106, 2, 30, "Subsection", Evaluatable->False], Cell[2692, 89, 932, 31, 127, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[3661, 125, 112, 2, 30, "Subsection", Evaluatable->False], Cell[3776, 129, 939, 31, 81, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[4752, 165, 92, 2, 30, "Subsection", Evaluatable->False], Cell[4847, 169, 903, 14, 158, "Text", Evaluatable->False], Cell[5753, 185, 314, 13, 31, "Text", Evaluatable->False], Cell[6070, 200, 479, 7, 237, "Input"], Cell[6552, 209, 324, 13, 31, "Text", Evaluatable->False], Cell[6879, 224, 508, 8, 222, "Input"], Cell[7390, 234, 314, 13, 31, "Text", Evaluatable->False], Cell[7707, 249, 350, 7, 177, "Input"] }, Closed]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)