(*********************************************************************** 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[ 24638, 730]*) (*NotebookOutlinePosition[ 25491, 762]*) (* CellTagsIndexPosition[ 25447, 758]*) (*WindowFrame->Normal*) Notebook[{ Cell[TextData["Procedural Programming"], "Title", Evaluatable->False, CellHorizontalScrolling->False, TextAlignment->Center], Cell[TextData[ "We give a few examples of programming in Mathematica. These programs \ illustrate\n the features of Mathematica discussed in the tutorial and \ section on lists. In addition,\n we also use some of Mathematica's string \ processing functions. These functions are\n introduced as needed."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Base b Representation"], "Subsection", Evaluatable->False], Cell[TextData[ " We write a program to find the digits in the base b representation of a \ positive\n integer. The problem is easy, because the lowest digit when n is \ represented in\n base b is the remainder when n is divided by b, and the \ higher order digits form the\n quotient. Hence we can write the following \ procedure that returns a list of the base b\n digits."], "Text", Evaluatable->False], Cell[TextData[ "basebdigits[n_,b_]:= Module[ { m=n, l={}},\n l= { \ Mod[ m,b]};\n m=Quotient[n,b];\n \ While[m>0,\n PrependTo[ l, Mod[m,b]];\n \ m=Quotient[m,b];\n ];\n \ Return[l]];"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["basebdigits[ 137,10]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 3, 7} \ \>", "\<\ {1, 3, 7}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["basebdigits[ 137,2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 0, 0, 0, 1, 0, 0, 1} \ \>", "\<\ {1, 0, 0, 0, 1, 0, 0, 1}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The first argument of Module is a list of variables used in the procedure. \ In this case, we use\n m and l (for the list of digits). The variables can be \ initialized in the first argument of Module, i.e.\n within the braces. The \ program can also be written recursively because we are \n computing the \ basebdigits of the quotient in the While loop. Here is another program that\n\ computes the base b digit expansion, this time, by calling itself with a \ smaller argument."], "Text", Evaluatable->False], Cell[TextData[ "newbasebdigits[n_,b_]:= Module[ {m=n, res= { Mod[n,b]}},\n \ m= Quotient[ m,b];\n If[ m>0,\n \ res=Join[newbasebdigits[m,b],res]];\n Return[res]]"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["newbasebdigits[137,2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {1, 0, 0, 0, 1, 0, 0, 1} \ \>", "\<\ {1, 0, 0, 0, 1, 0, 0, 1}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Instead of having the output as a list of digits, we may prefer to have it \ in the standard\n representation as a string, that is, 10001001 instead of { \ 1,0,0,0,1,0,0,1}. This can\n be done using the ToString command of \ Mathematica that will give the string form of\n an object, in this case, a \ string. We convert each digit to a String form and then join\n them using \ the StringJoin command. A list of String related commands is given in the \ next\n section."], "Text", Evaluatable->False], Cell[TextData[ "basebform[n_,b_]:= StringJoin[ \n Map[ ToString[ #] &,\ \n basebdigits[n,b]]]"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["basebform[ 137,2]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ \"10001001\" \ \>", "\<\ 10001001\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Write a program to take the string form of a number and return \ its value\n The Mathematica function Characters will return a list of \ characters and the command\n ToExpression will give the value of the string \ form of a digit. "], "Text", Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["String Manipulation "], "Subsection", Evaluatable->False], Cell[TextData[ " We develop some functions for manipulating strings. A string in \ Mathematica is\n a collection of characters in double quotes \". For example,\ \n\n s= \"abced01234567\" \n\n is a string. There are many functions for \ manipulating strings. Here is a brief list\n of the most useful functions."], "Text", Evaluatable->False], Cell[TextData[ "Characters[string] gives a list of characters in a string and\nStringJoin[ \ list] returns a string from the specified set of characters."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Characters[ \"abcdefg\"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"} \ \>", "\<\ {a, b, c, d, e, f, g}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["StringJoin[ %]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ \"abcdefg\" \ \>", "\<\ abcdefg\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The ASCII character codes for each character can be obtained from\n\ ToCharacterCode[ string]\n and the string form of a list of character codes \ is obtained using\nFromCharacterCode. These functions are useful is \ manipulating strings\n to perform arithmetic operations on them.\n\n \ Additional functions that are useful for String manipulation are the \ following.\n\nStringLength[ s] Gives the number of characters in s.\n\n\ StringTake[ s,k] Gives the first k characters of s.\n\nStringTake[ s, \ {k,l}] Gives the characters from positions k through l.\n\nStringPosition[ \ s, t] Gives a list of position where string t occurs in string s.\n\n\ StringDrop[ s, {k,l}] Returns a string with the characters from positions k \ through l of \n s removed.\n\n Strings can \ be joined using StringJoin[ s1,s2] or s1<>s2.\n\nStringReplace[ s, { \ s1->t1,....}] will replace all occurrences of s1 by t1 in s.\n\n We \ illustrate these with the following examples."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["Shift Cipher: "], "Subsubsection", Evaluatable->False], Cell[TextData[ " In the shift cipher, each character is represented by \n a number and then \ a fixed number (the key) is added to all the characters to form\n the cipher \ text. For simplicity, we remove all spaces and punctuation from\n the text \ and assume that every character is a lower case letter. This can be \ accomplished \n by the ToLowerCase command. \n\n We will convert a string to \ its character code and then add the constant number to\n it and then convert \ is back to the character code. "], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["ToCharacterCode[ \"abcdefghijklmnopqrstuvwxyz\"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122} \ \>", "\<\ {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "We see that the codes are consecutive. This will make the computation \ simpler. \n If we subtract 97 from the code, and then add number modulo 26, \ then add 97 to the number\n and convert it back to a string, then we will \ have the cipher text. This is done in the\n following procedure."], "Text", Evaluatable->False], Cell[TextData[ "shiftcipher[ s_, k_]:= Module[ {s1},\n \ s1=StringReplace[\n ToLowerCase[s], { \" \ \"->\"\"}];\n l=ToCharacterCode[ s1]-97;\n \ l=Mod[ l+k,26];\n \ s1=FromCharacterCode[l+97];\n Return[s1]]\n "], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["shiftcipher[ \"abc\",3]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ \"def\" \ \>", "\<\ def\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData[" shiftcipher[ \"I came I saw I conquered\", 7]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ \"pjhtlpzhdpjvuxblylk\" \ \>", "\<\ pjhtlpzhdpjvuxblylk\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The string should have no punctuation other than spaces. If you want to \ allow punctuation, then\n the StringReplace command in the procedure needs \ to modified appropriately. Since\n conversion of a string to numeric form is \ common, we will write a separate function for this."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData[ "ToNumericForm[m_String]:= Module[ {text},\n \ text=ToLowerCase[m];\n If[Length[StringPosition[text,\" \"]]>=1,\ \n text= StringReplace[text,\" \"->\"\"]];\n \ text=ToCharacterCode[text]-97;\n Return[text]]\n \n\ "], "Input", PageWidth->Infinity, InitializationCell->True, AspectRatioFixed->True, FontFamily->"Courier New", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]], Cell[OutputFormData["\<\ General::spell1: Possible spelling error: new symbol name \"text\" is similar to existing symbol \"Text\". \ \>", "\<\ General::spell1: Possible spelling error: new symbol name \"text\" is similar to existing symbol \"Text\".\ \>"], "Message", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["ToNumericForm[ \"GHIJKLM \"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {6, 7, 8, 9, 10, 11, 12} \ \>", "\<\ {6, 7, 8, 9, 10, 11, 12}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "\nWe also need the inverse of this operation, to convert a list of numbers \ modulo 26 to \n the string form."], "Text", Evaluatable->False], Cell[TextData["ToTextForm[ plain_List]:=FromCharacterCode[plain+97]"], "Input", PageWidth->Infinity, InitializationCell->True, AspectRatioFixed->True, FontFamily->"Courier New", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]]}, Open]], Cell[CellGroupData[{Cell[TextData["Frequency Analysis:"], "Subsubsection", Evaluatable->False], Cell[TextData[ " Many simple ciphers can be cryptanalyzed by frequency analysis, that is, \ counting\n the frequency of occurrence of each character and then comparing \ this to the known\n frequencies of the characters in English text. The same \ analysis can be performed \nin language or character set if Mathematica \ supports that character set.\n\n For the function, we use the String Position \ command to find the places that each\n character occurs in the string and \ then find the length of this list. Since we have to apply this\n to 26 \ letters of the alphabet, it is convenient to use Map and other list \n\ manipulation functions. The result will be a list with elements of the form\n\ { character,frequency}. "], "Text", Evaluatable->False], Cell[TextData[ "\n\n freq[str_]:= Transpose[ { Characters[\"abcdefghijklmnopqrstuvwxyz\"],\n\ Map[ Length[StringPosition[str,ToString[#]]] &,\n \ Characters[\"abcdefghijklmnopqrstuvwxyz\"]]\n }]\n "], "Input", PageWidth->Infinity, InitializationCell->True, AspectRatioFixed->True, FontFamily->"Courier New", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]], Cell[CellGroupData[{Cell[TextData["freq[ \"ghi djksiwlabc dksoabcndjdksueos\"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{\"a\", 2}, {\"b\", 2}, {\"c\", 2}, {\"d\", 4}, {\"e\", 1}, {\"f\", 0}, {\"g\", 1}, {\"h\", 1}, {\"i\", 2}, {\"j\", 2}, {\"k\", 3}, {\"l\", 1}, {\"m\", 0}, {\"n\", 1}, {\"o\", 2}, {\"p\", 0}, {\"q\", 0}, {\"r\", 0}, {\"s\", 4}, {\"t\", 0}, {\"u\", 1}, {\"v\", 0}, {\"w\", 1}, {\"x\", 0}, {\"y\", 0}, {\"z\", 0}} \ \>", "\<\ {{a, 2}, {b, 2}, {c, 2}, {d, 4}, {e, 1}, {f, 0}, {g, 1}, {h, 1}, {i, 2}, {j, 2}, {k, 3}, {l, 1}, {m, 0}, {n, 1}, {o, 2}, {p, 0}, {q, 0}, {r, 0}, {s, 4}, {t, 0}, {u, 1}, {v, 0}, {w, 1}, {x, 0}, {y, 0}, {z, 0}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "\nExercise: Use this function to determine the frequency of occurrence of \n\ letters in the English language. You should eliminate punctuation in the \ frequency analysis."], "Text", Evaluatable->False], Cell[TextData[ "Exercise: Write a function to Plot the frequency distribution. For this, you \ can\n modify the previous function to return a list of elements of the form\n \ { character code, frequency} where character code is a number between 0 and \ 25."], "Text", Evaluatable->False]}, Open]]}, Open]], Cell[CellGroupData[{Cell[TextData["Vigenere Cipher"], "Subsection", Evaluatable->False], Cell[TextData[ "The Vigenere Cipher is similar to the shift cipher but instead of adding a \ number\n to each character, it operates on blocks of characters. A secret key \ word of length k\n is added to blocks of k letters. To implement this cipher, \ we need a function that\n will break a list into sublists with k elements \ each. To implement this, we must ensure that\n the length is a multiple of k. \ This can be done by appending the necessary number of\n characters to the end \ of list when the string length is not a multiple of k. A function to\n \ break a text into blocks of k letters is given below. The built -in function \ Partition can also\n be used but we must ensure that the length is a multiple \ of k."], "Text", Evaluatable->False], Cell[TextData[ "breaktext[m_String, key_]:= Module[ {k=StringLength[key] },\n \ text=ToNumericForm[m];\n n=Length[text];\n \ plaintext={};\n While[ n>=k,\n \ AppendTo[plaintext,Take[text,k]];\n text=Drop[text,k];\n \ n=n-k;];\n If[ 0Infinity, AspectRatioFixed->True], Cell[CellGroupData[{Cell[TextData["breaktext[ \" abc def ghijklmop\", \"abcd\"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 14, 15, 15}} \ \>", "\<\ {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 14, 15, 15}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "\nIn the Vigenere cipher, we take a plaintext M and add a key word K as in \ the\n following example.\n\n Suppose M= I came I saw Iconquered and the key \ is Egypt,\n\n the we add the two strings\n\n IcameIsawIconquenred\n\ EgyptEgyptEgyptEgypt\n\nby doing arithmetic modulo 26. For this we generate \ the character codes of the two strings\n and then add them using the Map \ command. "], "Text", Evaluatable->False], Cell[TextData[ "\nvigenere[M_String, K_String]:= \n Module[ { l},\n \ \n l=breaktext[M,K];\n\n l=Mod[Map[ \ #+ToNumericForm[K] &,l],26];\n l=Flatten[l]+97;\n \ Return[FromCharacterCode[l]]]"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["\nvigenere[ \"I came Isaw I conquered\", \"Egypt\"]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{8, 2, 0, 12, 4}, {8, 18, 0, 22, 8}, {2, 14, 13, 16, 20}, {4, 17, 4, 3, 15}} {4, 6, 24, 15, 19} \ \>", "\<\ {{8, 2, 0, 12, 4}, {8, 18, 0, 22, 8}, {2, 14, 13, 16, 20}, {4, 17, 4, 3, 15}} {4, 6, 24, 15, 19}\ \>"], "Print", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ \"miybxmyylbgulfnixcsi\" \ \>", "\<\ miybxmyylbgulfnixcsi\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "The cryptanalysis of the Vigenere cipher also requires a frequency analysis \ of\n digrams and trigrams, two and three letter strings of the English \ alphabet. The program is\n similar to the frequency analysis of the letters \ of the alphabet that was given above.\n\nThe program finds all the two letter \ sequences in the text and counts their frequency. The second\n argument is an \ integer a that can be set to return only those digrams that occur at least\ \n a times. "], "Text", Evaluatable->False], Cell[TextData[ "digrams[cipher_,a_]:= Module[ {k=1,str,l,i,p, s={}},\n \ i=1;\n l=StringLength[cipher]; \n \ While[i< l-4 ,\n str=StringTake[cipher,{i,i+1}];\n \ p=Length[StringPosition[cipher,str]];\n If[p>a, \n\ AppendTo[s, {str,p}]];\n i++];\n \ Return[Union[s]]]\n "], "Input", PageWidth->Infinity, AspectRatioFixed->True, FontFamily->"Courier", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]], Cell[TextData[ "\nExercise: Rewrite the program without using a While statement. "], "Text",\ Evaluatable->False], Cell[TextData[ "Exercise: Modify the program to count the frequency of trigrams. Trigrams \ are sequences\n of three letters. The frequency analysis of trigrams is very \ useful because \"THE\" is the\n most common trigram in the English \ language."], "Text", Evaluatable->False]}, Open]], Cell[CellGroupData[{Cell[TextData["Sieve of Eratosthenes"], "Subsection", Evaluatable->False], Cell[TextData[ "The sieve of Eratosthenes is the classical method of generating prime \ numbers. The \n procedure is very easy to implement in Mathematica. We make a \ list of integers\n and remove the multiples of two, of three, and so on, \ until all we are left with are\n primes. If we generate a table of primes up \ to n, then it is necessary to eliminate\n the multiples of the primes up to \ square root of n."], "Text", Evaluatable->False], Cell[TextData[ "SieveOfEratosthenes[n_]:= \n Module[ {t,P,i=1,x},\n \ t=N[Sqrt[n]];\n P=Range[2,n];\n While[ P[[i]] <= t,\n \ \n x=Table[k P[[i]],\n {k,P[[i]], \ Floor[n/P[[i]]]}];\n \n P=Complement[P,x];\n \ i++];\n Return[P]]"], "Input", PageWidth->Infinity, AspectRatioFixed->True, FontFamily->"Courier", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]], Cell[CellGroupData[{Cell[TextData["SieveOfEratosthenes[100]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} \ \>", "\<\ {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Remarks: Do not use this sieve to generate primes for n more than 10^5 as \ it is\n likely to be very slow. For larger n, it is better to modify the \ sieve to generate \n primes between n1 and n2, by reading in a list of \ primes up to square root of n2.\n\n Also, it is better to save the result in \ a file rather than display it on a screen. This\n can be done by the >> \ operator and the file can be read in using << or the\n ReadList command."], "Text", Evaluatable->False], Cell[TextData["SieveOfEratosthenes[ 1000]>> primes;"], "Input", PageWidth->Infinity], Cell[TextData["\nprimes= ReadList[ \"primes\", Expression];"], "Input", PageWidth->Infinity], Cell[CellGroupData[{Cell[TextData["primes"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ {{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}} \ \>", "\<\ {{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Notice the extra set of braces around the expression. These can be removed\n\ using the Flatten command."], "Text", Evaluatable->False], Cell[CellGroupData[{Cell[TextData["primes=Flatten[primes];\nprimes[[50]]"], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ 229 \ \>", "\<\ 229\ \>"], "Output", PageWidth->Infinity, Evaluatable->False]}, Open]], Cell[TextData[ "Exercise: Write a function Sieve[n,m] to create a list of primes between n \ and m. To start\n the sieve you will need a list of primes up to square root \ of m."], "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, 130, 3, 70, "Title", Evaluatable->False], Cell[1844, 56, 338, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[2205, 63, 75, 1, 70, "Subsection", Evaluatable->False], Cell[2283, 66, 411, 6, 70, "Text", Evaluatable->False], Cell[2697, 74, 398, 6, 70, "Input"], Cell[CellGroupData[{ Cell[3118, 82, 70, 1, 70, "Input"], Cell[3191, 85, 122, 11, 70, "Output", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[3345, 98, 69, 1, 70, "Input"], Cell[3417, 101, 152, 11, 70, "Output", Evaluatable->False] }, Closed]], Cell[3581, 114, 531, 8, 70, "Text", Evaluatable->False], Cell[4115, 124, 274, 5, 70, "Input"], Cell[CellGroupData[{ Cell[4412, 131, 71, 1, 70, "Input"], Cell[4486, 134, 152, 11, 70, "Output", Evaluatable->False] }, Closed]], Cell[4650, 147, 520, 8, 70, "Text", Evaluatable->False], Cell[5173, 157, 177, 3, 70, "Input"], Cell[CellGroupData[{ Cell[5373, 162, 67, 1, 70, "Input"], Cell[5443, 165, 124, 11, 70, "Output", Evaluatable->False] }, Closed]], Cell[5579, 178, 292, 5, 70, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[5903, 185, 74, 1, 70, "Subsection", Evaluatable->False], Cell[5980, 188, 348, 6, 70, "Text", Evaluatable->False], Cell[6331, 196, 190, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[6544, 201, 74, 1, 70, "Input"], Cell[6621, 204, 172, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[6825, 215, 64, 1, 70, "Input"], Cell[6892, 218, 120, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[7024, 229, 1057, 15, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[8104, 246, 71, 1, 70, "Subsubsection", Evaluatable->False], Cell[8178, 249, 549, 8, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[8750, 259, 98, 1, 70, "Input"], Cell[8851, 262, 380, 17, 70, "Output", Evaluatable->False] }, Closed]], Cell[9243, 281, 342, 5, 70, "Text", Evaluatable->False], Cell[9588, 288, 399, 7, 70, "Input"], Cell[CellGroupData[{ Cell[10010, 297, 73, 1, 70, "Input"], Cell[10086, 300, 112, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[10230, 311, 96, 1, 70, "Input"], Cell[10329, 314, 144, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[10485, 325, 331, 5, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[10839, 332, 532, 13, 70, "Input", InitializationCell->True], Cell[11374, 347, 332, 12, 70, "Message", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[11738, 361, 78, 1, 70, "Input"], Cell[11819, 364, 150, 9, 70, "Output", Evaluatable->False] }, Closed]], Cell[11981, 375, 158, 3, 70, "Text", Evaluatable->False], Cell[12142, 380, 278, 8, 70, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[12452, 390, 76, 1, 70, "Subsubsection", Evaluatable->False], Cell[12531, 393, 765, 11, 70, "Text", Evaluatable->False], Cell[13299, 406, 435, 11, 70, "Input", InitializationCell->True], Cell[CellGroupData[{ Cell[13757, 419, 93, 1, 70, "Input"], Cell[13853, 422, 664, 23, 70, "Output", Evaluatable->False] }, Closed]], Cell[14529, 447, 223, 4, 70, "Text", Evaluatable->False], Cell[14755, 453, 289, 5, 70, "Text", Evaluatable->False] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[15085, 460, 69, 1, 70, "Subsection", Evaluatable->False], Cell[15157, 463, 766, 11, 70, "Text", Evaluatable->False], Cell[15926, 476, 601, 10, 70, "Input"], Cell[CellGroupData[{ Cell[16550, 488, 94, 1, 70, "Input"], Cell[16647, 491, 231, 10, 70, "Output", Evaluatable->False] }, Closed]], Cell[16890, 503, 437, 7, 70, "Text", Evaluatable->False], Cell[17330, 512, 312, 5, 70, "Input"], Cell[CellGroupData[{ Cell[17665, 519, 101, 1, 70, "Input"], Cell[17769, 522, 298, 12, 70, "Print", Evaluatable->False], Cell[18070, 536, 145, 8, 70, "Output", Evaluatable->False] }, Closed]], Cell[18227, 546, 531, 8, 70, "Text", Evaluatable->False], Cell[18761, 556, 634, 13, 70, "Input"], Cell[19398, 571, 117, 3, 70, "Text", Evaluatable->False], Cell[19518, 576, 282, 5, 70, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[19832, 583, 75, 1, 70, "Subsection", Evaluatable->False], Cell[19910, 586, 451, 7, 70, "Text", Evaluatable->False], Cell[20364, 595, 526, 12, 70, "Input"], Cell[CellGroupData[{ Cell[20913, 609, 74, 1, 70, "Input"], Cell[20990, 612, 305, 12, 70, "Output", Evaluatable->False] }, Open ]], Cell[21307, 626, 507, 8, 70, "Text", Evaluatable->False], Cell[21817, 636, 86, 1, 70, "Input"], Cell[21906, 639, 94, 1, 70, "Input"], Cell[CellGroupData[{ Cell[22023, 642, 56, 1, 70, "Input"], Cell[22082, 645, 1935, 60, 70, "Output", Evaluatable->False] }, Open ]], Cell[24029, 707, 154, 3, 70, "Text", Evaluatable->False], Cell[CellGroupData[{ Cell[24206, 712, 87, 1, 70, "Input"], Cell[24296, 715, 107, 8, 70, "Output", Evaluatable->False] }, Open ]], Cell[24415, 725, 211, 4, 70, "Text", Evaluatable->False] }, Closed]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)