(*********************************************************************** 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[ 25539, 957]*) (*NotebookOutlinePosition[ 26392, 989]*) (* CellTagsIndexPosition[ 26348, 985]*) (*WindowFrame->Normal*) Notebook[{ Cell[TextData["Lists"], "Title", Evaluatable->False, CellHorizontalScrolling->False, TextAlignment->Center], Cell[CellGroupData[{Cell[TextData["List Construction"], "Subsection", Evaluatable->False], Cell[TextData[ "We start with a description of the basic list building commands. The \ simplest are \n Range and Table. The first, Range, generates a list of \ numbers in arithmetic\n progression. The format is\n\n Range[ min, \ max,step]"], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Range[ 1, 90, 8]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89} \ \>", "\<\ {1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Range[ 0.1, 3.0, 0.6]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {0.1, 0.7, 1.3, 1.9, 2.5} \ \>", "\<\ {0.1, 0.7, 1.3, 1.9, 2.5}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The Table command is more general as it can take a function as its first \ argument."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Table[ i^2, { i, 1, 5}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 4, 9, 16, 25} \ \>", "\<\ {1, 4, 9, 16, 25}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData["Table can take more than one iterator."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Table[ x^2+y^2, {x,1,3}, {y,1,4}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{2, 5, 10, 17}, {5, 8, 13, 20}, {10, 13, 18, 25}} \ \>", "\<\ {{2, 5, 10, 17}, {5, 8, 13, 20}, {10, 13, 18, 25}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The result is a list of lists in which the first list has x fixed to 1 and \ y varies from 1 to\n 4, then x is fixed to 2 and so on. The additional braces \ can be removed by using\n the Flatten command."], "Text", Evaluatable->False], Cell[TextData[ " We can add elements to an existing list by using the Append, Prepend and \ Insert\n commands. To combine two lists, use the Union and Join commands. "], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "list= {};\nlist= Append[ list, 3]\nlist= Append[ list, 5]\n"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ General::spell1: Possible spelling error: new symbol name \"list\" is similar to existing symbol \"List\". \ \>", "\<\ General::spell1: Possible spelling error: new symbol name \"list\" is similar to existing symbol \"List\".\ \>"], "Message", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {3} \ \>", "\<\ {3}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {3, 5} \ \>", "\<\ {3, 5}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "We see that Append adds the element to the end of the list. Append \ returns\n a list with the element added, but does not modify the argument. \ For example,\n list1= Append[ list, 7] will not modify list."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["list1= Append[ list, 7];\n list\nlist1"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {3, 5} \ \>", "\<\ {3, 5}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {3, 5, 7} \ \>", "\<\ {3, 5, 7}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ " To modify the argument so that an additional assignment is not necessary, \ use\n the AppendTo command.\n AppendTo[ list, element] will add the element \ to the list.\n\n The corresponding functions for inserting elements at the \ beginning of a list are\n Prepend and PrependTo. \n\n To insert an element \ into a list at an arbitrary position, use Insert. The structure\n is \ Insert[list,element, position]. If the specified position is a negative \ integer, then the element\n is inserted from the end of the list."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["list= { 3, 5, 7, 13};\nlist1= Insert[ list, 11, -2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {3, 5, 7, 11, 13} \ \>", "\<\ {3, 5, 7, 11, 13}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "To combine lists, use the Union and Join commands. Union removes any \ duplicates \n and sorts the result while Join appends the second list to \ the end of the first.."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "list1= { 1,1,2,3,5,8,13,21};\nlist2= { 3, 5, 7,11,13};\nUnion[ list1, list2]\ \nJoin[list1,list2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 2, 3, 5, 7, 8, 11, 13, 21} \ \>", "\<\ {1, 2, 3, 5, 7, 8, 11, 13, 21}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {1, 1, 2, 3, 5, 8, 13, 21, 3, 5, 7, 11, 13} \ \>", "\<\ {1, 1, 2, 3, 5, 8, 13, 21, 3, 5, 7, 11, 13}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]]}, Open]], Cell[CellGroupData[{Cell[TextData["List Manipulation"], "Subsection", Evaluatable->False], Cell[TextData[ " To access an element of a list we can use the Part command or the [[ ]] \ operator.\nPart[ list, k] gives the kth element of the list."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["list= { 1,2,4,8,16,32};\nPart[ list,4]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ 8 \ \>", "\<\ 8\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["list[[5]]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ 16 \ \>", "\<\ 16\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "To obtain more than one element, use Take. \nTake[ list, k] makes a new \ list out of the first k elements of the argument. If k\n is negative then the \ last k elements are taken. To obtain the sublist of elements\n from position \ i through position j, use the command\n Take[ list, {i,j}]\n Similarly, \ Drop [list,k] makes a new\n list by removing the first k elements ( or last \ |k| elements if k is negative)."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["list= { 1,3,5,7,9,10}\nTake[ list,2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 3, 5, 7, 9, 10} \ \>", "\<\ {1, 3, 5, 7, 9, 10}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {1, 3} \ \>", "\<\ {1, 3}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Take[ list, { 2, 3}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {3, 5} \ \>", "\<\ {3, 5}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "We illustrate this with an example program which takes a list and returns \ a new list\n consistingof pairs of elements from the first list. If the \ input is { 1,3,5,7,9,11},\n then the output is { { 1,3}, { 5,7}, { 9,11} }."], "Text", Evaluatable->False], Cell[TextData[ "makepairs[l_]:=Module[ {newlist={},i=1},\n While[i< \ Length[l],\n AppendTo[newlist,\n \ Take[l, {i,i+1}]];\n i=i+2;];\n \ Return[newlist]]\n \n \ \n "], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["makepairs[ { 1,3,5,7,9,11}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{1, 3}, {5, 7}, {9, 11}} \ \>", "\<\ {{1, 3}, {5, 7}, {9, 11}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["makepairs[ { 1,3,5,7,9,11,13}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{1, 3}, {5, 7}, {9, 11}} \ \>", "\<\ {{1, 3}, {5, 7}, {9, 11}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Notice that if the list has an odd number of elements, then the \ last element\n is dropped. Modify the program so that the last element is \ included at the end of the \n resulting list, possibly by adding a zero to \ the list if the length is odd."], "Text", Evaluatable->False], Cell[TextData[ "The same result can also be obtained by the Mathematica function Partition.\n\ Partition[ list,n] breaks the list into sublists of n elements."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Partition[ { 1,3,5,7,9,11,13}, 2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{1, 3}, {5, 7}, {9, 11}} \ \>", "\<\ {{1, 3}, {5, 7}, {9, 11}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Modify the function above to write a function that Partitions a \ list into sublists\n of n elements. If the number of elements is not a \ multiple of n, then add enough zeros\n to make it a multiple of n."], "Text", Evaluatable->False], Cell[TextData[ "If a list has sublists, then the elements can be accessed using the Part \ or [[ ]] commands.\n The ith sublist can be obtained by [[i]], and then the \ jth element of this can be accessed.\n A shortcut is to use [[ i,j]], which \ returns the jth element of the ith sublist."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["list= { { 1,3}, { 5,7}, { 9, 11}};\nlist[[ 2,2]]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ 7 \ \>", "\<\ 7\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "A list consisting of sublists of equal length is a matrix, and matrix \ operations can be\n applied to this. Transpose interchanges the rows and \ columns of a matrix. Recall that\n FactorInteger returns the prime \ factorization of an integer, consisting of a list of lists,\n where each \ sublist is of the form { p, e} where p^e is the term corresponding to\n p in \ the prime factorization of n. Using Transpose, we can write a function to \ extract \n the primefactors of an integer."], "Text", Evaluatable->False], Cell[TextData[ "primefactors[n_]:= First[Transpose[ FactorInteger[n]]];"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["primefactors[ 879312]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {2, 3, 7, 2617} \ \>", "\<\ {2, 3, 7, 2617}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Write a function to extract all the exponents in the prime \ factorization. A number\n n is squarefree if all the exponents are equal to \ 1. Write a function to check if all the exponents\n are 1, and hence to \ detect if a number is squarefree."], "Text", Evaluatable->False], Cell[TextData[ "Additional list manipulation functions are Union, Join, Intersection, and \ Complement. We \n discussed Union and Join in the previous section. \ Complement has the following\n format.\nComplement[ universal, list1,list2, \ ..] removes all occurrences of elements\n of list1, list2, and so on from \ universal."], "Text", Evaluatable->False], Cell[TextData[ "Suppose we wish to find the number of integers less than 300 that are \ multiples of 3\n but are not multiples of 7. We can make a table of the \ multiples of 3 and 7, then\n use Complement and count the number. (There are \ better ways than this, but it does\n illustrate the use of these \ functions)."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "a= Table[ 3i, { i, 1, Floor[ 300/3]}];\nb=Table[ 7 i, { i, 1,Floor[ \ 300/7]}];\nresult=Length[ Complement[ a,b]]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ 86 \ \>", "\<\ 86\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData["Exercise: Write a function to reverse a list."], "Text", Evaluatable->False], Cell[TextData[ "Exercise: Write a function to rotate a list by a specified number of \ elements either\n clockwise or counterclockwise. For example, the result of \ rotating\n { 1,2,3,4,5,6,7} by 2 elements should be { 3,4,5,6,7,1,2}."], "Text", Evaluatable->False]}, Open]], Cell[TextData["Applying Functions to Lists"], "Subsection", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[" Map, Apply,and Fold "], "Subsubsection", Evaluatable->False], Cell[TextData[ "Mathematica has a convenient mechanism to apply functions to lists. This \ is a\n very powerful feature and usually much faster than the Do or For \ loops. The simplest\n way to apply a function to a list is to make the \ function listable. This is done using\n SetAttributes command."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "square[x_]:=x^2;\nSetAttributes[square, Listable]\n\nsquare[ { \ 2,5,7,9,11}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {4, 25, 49, 81, 121} \ \>", "\<\ {4, 25, 49, 81, 121}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Making square a listable function allows it to be applied to each element \ of a list. Notice\n that there is no need for loops as in other programming \ languages. Most of Mathematica's\nbuilt-in functions are Listable and you can \ make your functions Listable by using the\n SetAttributes command."], "Text", Evaluatable->False], Cell[TextData[ " In the previous example we defined a function square and made it \ listable. Sometimes\n we want to do an operation on a list only a few times, \ so it is wasteful to define \n additional functions. Mathematica has a \ convenient mechanism to define a function \n inline (using the pure function \ command) and apply it to lists. This is done via the Map \n command. "], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Map[ #^2 &, { 3,5,7,11}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {9, 25, 49, 121} \ \>", "\<\ {9, 25, 49, 121}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ " Map takes two arguments, the first is a function and the second a list. In \ the above example,\n the function is #^2 &, where # is the variable. The \ function returns the square of the\n variable. The ampersand signifies that \ it is pure function, so thereis no need for the name\n of the variable. The \ function is applied to each element. The general format of Map is"], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Map[ f, { A,B,C}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {f[A], f[B], f[C]} \ \>", "\<\ {f[A], f[B], f[C]}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ " To sum the elements of a list, we use the Apply command.\n Apply[ \ operator, list] applies the operator to all the elements of the list. \ Usually, the \n operator is Plus or Times, so that we get the sum or product \ of the elements of the\n List. "], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Clear[a,b,c]\nApply[Plus, {a,b,c}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ a + b + c \ \>", "\<\ a + b + c\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Apply[Times, {a,b,c}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ a*b*c \ \>", "\<\ a b c\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Recall, that we wrote a program to determine the prime factors of an \ integer.\n Similarly, we can obtain the list of exponents in the prime \ factorization. To check\n if a number is squarefree, we have to check that \ the exponents are all one. One way\n to do this is to multiply all the \ exponents and see if the product equals one. The\n following program returns \ True if an integer is squarefree and False otherwise. It is\n limited by a \ limits of FactorInteger, so it will not work for numbers that have more\n \ than 12-15 digits."], "Text", Evaluatable->False], Cell[TextData[ "squarefreeQ[n_]:= Apply[ Times,\n \ Last[Transpose[FactorInteger[n]]]\n ]==1;"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["squarefreeQ[ 21]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ True \ \>", "\<\ True\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["squarefreeQ[75]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ False \ \>", "\<\ False\ \>"], "Output", PageWidth->Infinity, Evaluatable->False], Cell[TextData[ "Exercise: Write a function to return the number of divisors of an integer. \ If {e1,e2,e3,..}\n is the list of exponents in the prime factorization, then \ the number of positive divisors\n is (e1+1) (e2+1)(e3+1) ....\nDon't use any \ loops in your program. An appropriate combination of Map and Apply\n is \ sufficient."], "Text", Evaluatable->False]}, Open]], Cell[TextData[ "\nTwo more functions that are very useful are Fold and FoldList. Using \ Map, Apply, Fold and\n FoldList one can create very short programs to do some \ complex operations, programs\n that would be hundreds of lines long in \ languages such as C. \n\nThe effect of Fold and FoldList is clear from the \ following, which also shows their usage."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Fold [ f, x, {a,b,c}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ f[f[f[x, a], b], c] \ \>", "\<\ f[f[f[x, a], b], c]\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["FoldList[ f, x, {a,b,c}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {x, f[x, a], f[f[x, a], b], f[f[f[x, a], b], c]} \ \>", "\<\ {x, f[x, a], f[f[x, a], b], f[f[f[x, a], b], c]}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ " We illustrate the use of these functions by writing a function that returns \ the divisors\n of an integer. To determine the divisors, we first obtain the \ prime factorization. This consists\n of a list of elements {p,e}, where p is \ a prime of exponent e. From this we make a list\n of powers of p and then \ multiply everything together to make a list of divisors.. Then we write a \ function to\n multiply two lists, and then put everything together using \ Fold."], "Text", Evaluatable->False], Cell[TextData[ "primepowers[n_]:=\n Map [Table[ #[[1]]^i, { i,0,#[[2]]}] &,\n \ FactorInteger[n]]"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["primepowers[24]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{1, 2, 4, 8}, {1, 3}} \ \>", "\<\ {{1, 2, 4, 8}, {1, 3}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "multiplylists[ l1_,l2_]:=\n Union[ Flatten[ Map[ l1 # &,l2]]]"], "Input",\ PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["multiplylists[ {1,2,4,8}, {1,3}]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 2, 3, 4, 6, 8, 12, 24} \ \>", "\<\ {1, 2, 3, 4, 6, 8, 12, 24}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Now, we have the two basic functions to be combined to get all the \ divisors. This can\n be done using Fold."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "divisors[n_]:= Fold[ multiplylists, {1},\n \ primepowers[n]]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ General::spell1: Possible spelling error: new symbol name \"divisors\" is similar to existing symbol \"Divisors\". \ \>", "\<\ General::spell1: Possible spelling error: new symbol name \"divisors\" is similar to existing symbol \"Divisors\".\ \>"], "Message", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["divisors[24]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 2, 3, 4, 6, 8, 12, 24} \ \>", "\<\ {1, 2, 3, 4, 6, 8, 12, 24}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["divisors[ 315]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 3, 5, 7, 9, 15, 21, 35, 45, 63, 105, 315} \ \>", "\<\ {1, 3, 5, 7, 9, 15, 21, 35, 45, 63, 105, 315}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ " This simple function shows the power and elegance of Mathematica. Imagine \ doing the\n same in C."], "Text", Evaluatable->False], Cell[TextData[ "Exercise: Write a function to find the sum and product of all the divisors. \ Write another\n function to compute the sum of the kth powers of the divisors \ of an integer."], "Text", Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Selecting Member of a List"], "Subsubsection", Evaluatable->False], Cell[TextData[ "There are a few other functions that are useful in manipulating lists. \ These allow us Select\n elements of a list satisfying a specified condition. \ The following four functions are important.\n\nMemberQ[ list, element] \ returns True if the element occurs in the list.\n\nSelect[ list, condition] \ returns a list of elements of list satisfying the condition\n\n Position[ \ list, element] returns a list of all places that the element occurs in \ the list.\n\nCount[ list, element] returns the number of occurrences of \ element in list."], "Text", Evaluatable->False], Cell[TextData[ "\nSuppose we wish to select primes from a list of integers. Then we can use \ Select with \n PrimeQ."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Select[ { 3, 7, 9, 17, 127, 315}, PrimeQ]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {3, 7, 17, 127} \ \>", "\<\ {3, 7, 17, 127}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["MemberQ[ { 3,7,8, 9, 11}, 9]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ True \ \>", "\<\ True\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Position[ { 3, 7, 7, 8, 9, 7, 8, 0}, 7]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{2}, {3}, {6}} \ \>", "\<\ {{2}, {3}, {6}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Write a function to make a list of numbers of the form 5k+3 that \ are prime.\n Write another function to make a list of numbers of the form \ 7k+1 that are squarefree.\n You can use the function squarefreeQ that was \ written in the previous section."], "Text", Evaluatable->False], Cell[TextData[ "Exercise: Recall that we had a function squareQ that checks if a number is \ a perfect\n square by computing its real square root, taking the integer \ part, squaring it again to see\n if it equals the number. This is \ inefficient, and can be made more efficient by checking\n the remainder when \ divided by different integers. For example, a perfect square that\n is \ divided by 16 has a remainder of 0, 1, 4, or 9. Use this fact to write a \ function\n newsquareQ that first checks if Mod[ n, 16] is in the list { \ 0,1,4,9} before\n applying the older test. This will eliminate the square \ root computation is 3/4 of the inputs."], "Text", Evaluatable->False]}, Open]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowToolbars->{}, CellGrouping->Manual, WindowSize->{520, 600}, WindowMargins->{{238, Automatic}, {Automatic, 152}}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False} ] (*********************************************************************** 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[1711, 51, 113, 3, 70, "Title", Evaluatable->False], Cell[CellGroupData[{ Cell[1847, 56, 71, 1, 70, "Subsection", Evaluatable->False], Cell[1921, 59, 272, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[2216, 66, 66, 1, 70, "Input"], Cell[2285, 69, 195, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[2512, 81, 71, 1, 70, "Input"], Cell[2586, 84, 153, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[2751, 96, 133, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[2907, 101, 73, 1, 70, "Input"], Cell[2983, 104, 137, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[3132, 116, 87, 1, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[3242, 119, 83, 1, 70, "Input"], Cell[3328, 122, 209, 12, 70, "Output", Evaluatable->False] }, Open ]], Cell[3549, 136, 254, 4, 70, "Text", Evaluatable->False], Cell[3806, 142, 203, 4, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[4032, 148, 110, 2, 70, "Input"], Cell[4145, 152, 334, 14, 70, "Message", Evaluatable->False], Cell[4482, 168, 109, 10, 70, "Output", Evaluatable->False], Cell[4594, 180, 115, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[4721, 192, 259, 4, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[5003, 198, 88, 1, 70, "Input"], Cell[5094, 201, 115, 10, 70, "Output", Evaluatable->False], Cell[5212, 213, 121, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[5345, 225, 571, 8, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[5939, 235, 101, 1, 70, "Input"], Cell[6043, 238, 137, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[6192, 250, 223, 4, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[6438, 256, 148, 3, 70, "Input"], Cell[6589, 261, 163, 10, 70, "Output", Evaluatable->False], Cell[6755, 273, 189, 10, 70, "Output", Evaluatable->False] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[6985, 285, 71, 1, 70, "Subsection", Evaluatable->False], Cell[7059, 288, 187, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[7269, 293, 88, 1, 70, "Input"], Cell[7360, 296, 105, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[7497, 308, 59, 1, 70, "Input"], Cell[7559, 311, 107, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[7678, 323, 469, 7, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[8170, 332, 86, 1, 70, "Input"], Cell[8259, 335, 141, 10, 70, "Output", Evaluatable->False], Cell[8403, 347, 115, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[8550, 359, 70, 1, 70, "Input"], Cell[8623, 362, 115, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[8750, 374, 280, 5, 70, "Text", Evaluatable->False], Cell[9033, 381, 396, 6, 70, "Input"], Cell[CellGroupData[{ Cell[9452, 389, 77, 1, 70, "Input"], Cell[9532, 392, 153, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[9717, 404, 80, 1, 70, "Input"], Cell[9800, 407, 153, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[9965, 419, 309, 5, 70, "Text", Evaluatable->False], Cell[10277, 426, 193, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[10493, 431, 83, 1, 70, "Input"], Cell[10579, 434, 153, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[10744, 446, 264, 4, 70, "Text", Evaluatable->False], Cell[11011, 452, 326, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[11360, 459, 98, 1, 70, "Input"], Cell[11461, 462, 105, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[11578, 474, 541, 8, 70, "Text", Evaluatable->False], Cell[12122, 484, 106, 2, 70, "Input"], Cell[CellGroupData[{ Cell[12251, 488, 71, 1, 70, "Input"], Cell[12325, 491, 133, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[12470, 503, 306, 5, 70, "Text", Evaluatable->False], Cell[12779, 510, 362, 6, 70, "Text", Evaluatable->False], Cell[13144, 518, 353, 6, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[13520, 526, 164, 3, 70, "Input"], Cell[13687, 531, 107, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[13806, 543, 93, 1, 70, "Text", Evaluatable->False], Cell[13902, 546, 270, 5, 70, "Text", Evaluatable->False] }, Closed]], Cell[14184, 553, 81, 1, 70, "Subsection", Evaluatable->False], Cell[CellGroupData[{ Cell[14288, 556, 78, 1, 70, "Subsubsection", Evaluatable->False], Cell[14369, 559, 337, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[14729, 566, 128, 3, 70, "Input"], Cell[14860, 571, 143, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[15015, 583, 350, 5, 70, "Text", Evaluatable->False], Cell[15368, 590, 422, 7, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[15813, 599, 74, 1, 70, "Input"], Cell[15890, 602, 135, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[16037, 614, 426, 6, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[16486, 622, 67, 1, 70, "Input"], Cell[16556, 625, 139, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[16707, 637, 302, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[17032, 644, 84, 1, 70, "Input"], Cell[17119, 647, 121, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[17272, 659, 71, 1, 70, "Input"], Cell[17346, 662, 113, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[17471, 674, 593, 9, 70, "Text", Evaluatable->False], Cell[18067, 685, 175, 3, 70, "Input"], Cell[CellGroupData[{ Cell[18265, 690, 66, 1, 70, "Input"], Cell[18334, 693, 111, 10, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[18477, 705, 65, 1, 70, "Input"], Cell[18545, 708, 113, 10, 70, "Output", Evaluatable->False], Cell[18661, 720, 370, 6, 70, "Text", Evaluatable->False] }, Open ]], Cell[19043, 728, 395, 6, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[19461, 736, 71, 1, 70, "Input"], Cell[19535, 739, 140, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[19707, 750, 74, 1, 70, "Input"], Cell[19784, 753, 198, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[19994, 764, 519, 8, 70, "Text", Evaluatable->False], Cell[20516, 774, 138, 3, 70, "Input"], Cell[CellGroupData[{ Cell[20677, 779, 65, 1, 70, "Input"], Cell[20745, 782, 146, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[20903, 793, 118, 3, 70, "Input"], Cell[CellGroupData[{ Cell[21044, 798, 82, 1, 70, "Input"], Cell[21129, 801, 154, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[21295, 812, 160, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[21478, 817, 132, 3, 70, "Input"], Cell[21613, 822, 359, 15, 70, "Message", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[22004, 839, 62, 1, 70, "Input"], Cell[22069, 842, 154, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[22255, 853, 64, 1, 70, "Input"], Cell[22322, 856, 192, 9, 70, "Output", Evaluatable->False] }, Open ]], Cell[22526, 867, 148, 3, 70, "Text", Evaluatable->False], Cell[22677, 872, 222, 4, 70, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[22931, 878, 83, 1, 70, "Subsubsection", Evaluatable->False], Cell[23017, 881, 612, 9, 70, "Text", Evaluatable->False], Cell[23632, 892, 150, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[23805, 897, 91, 1, 70, "Input"], Cell[23899, 900, 132, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[24063, 911, 78, 1, 70, "Input"], Cell[24144, 914, 110, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[24286, 925, 89, 1, 70, "Input"], Cell[24378, 928, 132, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[24522, 939, 315, 5, 70, "Text", Evaluatable->False], Cell[24840, 946, 687, 10, 70, "Text", Evaluatable->False] }, Closed]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)