(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing 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[ 11541, 495]*) (*NotebookOutlinePosition[ 12171, 517]*) (* CellTagsIndexPosition[ 12127, 513]*) (*WindowFrame->Normal*) Notebook[{ Cell[BoxData[ \(Needs["\"]\)], "Input"], Cell[CellGroupData[{ Cell["Miller-Rabin-Selfridge Probabilistic Primality Test", "Title"], Cell[TextData[{ "The Miller-Rabin-Selfridge test is described in the following steps:\n\n\ Given a natural number ", Cell[BoxData[ \(TraditionalForm\`n = 1 + \(2\^t\) m\)]], " where ", StyleBox["m", FontSlant->"Italic"], " is and odd natural number and ", StyleBox["t", FontSlant->"Italic"], "\[Element]\[DoubleStruckCapitalN], and a parameter r\[Element]\ \[DoubleStruckCapitalN],\n\n(1) Choose a random integer ", StyleBox["a", FontSlant->"Italic"], " with 2\[LessEqual]", StyleBox["a", FontSlant->"Italic"], "\[LessEqual]", StyleBox["n", FontSlant->"Italic"], "-2.\n(2) If ", Cell[BoxData[ \(TraditionalForm\`\(\(a\^m\)\(\[Congruent]\)\)\)]], "1(mod ", StyleBox["n", FontSlant->"Italic"], ") then return to step (1).\n(3) Compute ", Cell[BoxData[ \(TraditionalForm\`\(a\^\(\(2\^j\) m\)\)(mod\ n)\)]], " for 0\[LessEqual]", StyleBox["j", FontSlant->"Italic"], "\[LessEqual]", StyleBox["t", FontSlant->"Italic"], "-1. If none of the congruences equals -1, ", "then ", StyleBox["n", FontSlant->"Italic"], " is composite.\n(4) Decrement ", StyleBox["r", FontSlant->"Italic"], " and return to (1)." }], "Text"], Cell[CellGroupData[{ Cell["Example 1", "Section"], Cell["\<\ The integer 3539 is prime so it should pass the \ Miller-Rabin-Selfridge test.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(PrimeQ[n = 3539]\)], "Input"], Cell[BoxData[ \(True\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(t = 0; m = n - 1; While[EvenQ[m], m = m/2; t = t + 1]; {t, m}\)], "Input"], Cell[BoxData[ \({1, 1769}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(n \[Equal] 1 + m*2^t\)], "Input"], Cell[BoxData[ \(True\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell["Step (1)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(a = Random[Integer, {2, n - 2}]\)], "Input"], Cell[BoxData[ \(3206\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Step (2)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(x = PowerMod[a, m, n]\)], "Input"], Cell[BoxData[ \(1\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Step (3)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(NestList[Mod[#*#, n] &, x, t - 1]\)], "Input"], Cell[BoxData[ \({1}\)], "Output"] }, Open ]], Cell[TextData[{ "Since this list contains no element congruent to 1 modulo ", StyleBox["n", FontSlant->"Italic"], ", then ", StyleBox["n", FontSlant->"Italic"], " is probably prime (in fact it is prime)." }], "Text"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Example 2", "Section"], Cell["\<\ The integer 11137 is composite so it may fail the \ Miller-Rabin-Selfridge test.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(PrimeQ[n = 11137]\)], "Input"], Cell[BoxData[ \(False\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(t = 0; m = n - 1; While[EvenQ[m], m = m/2; t = t + 1]; {t, m}\)], "Input"], Cell[BoxData[ \({7, 87}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(n \[Equal] 1 + m*2^t\)], "Input"], Cell[BoxData[ \(True\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell["Step (1)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(a = Random[Integer, {2, n - 2}]\)], "Input"], Cell[BoxData[ \(8152\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Step (2)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(x = PowerMod[a, m, n]\)], "Input"], Cell[BoxData[ \(11110\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Step (3)", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(NestList[Mod[#*#, n] &, x, t - 1]\)], "Input"], Cell[BoxData[ \({11110, 729, 8002, 5391, 6448, 2283, 11110}\)], "Output"] }, Open ]], Cell[TextData[{ "Since this list contains no element congruent to -1 modulo ", StyleBox["n", FontSlant->"Italic"], ", then ", StyleBox["n", FontSlant->"Italic"], " is composite." }], "Text"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Example 3", "Section"], Cell[CellGroupData[{ Cell[BoxData[ \(MillerRabinSelfridge[3539, 5, Verbose \[Rule] True]\)], "Input"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]3538\), SequenceForm[ "Step 2: ", 3538], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{3538}\), SequenceForm[ "Step 3: ", {3538}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]1\), SequenceForm[ "Step 2: ", 1], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]3538\), SequenceForm[ "Step 2: ", 3538], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{3538}\), SequenceForm[ "Step 3: ", {3538}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]1\), SequenceForm[ "Step 2: ", 1], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]3538\), SequenceForm[ "Step 2: ", 3538], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{3538}\), SequenceForm[ "Step 3: ", {3538}], Editable->False]], "Print"], Cell[BoxData[ \({"Prime", {1769, 1}, {611, 1877, 2437, 121, 1094}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MillerRabinSelfridge[3539, {2, 16, 764}]\)], "Input"], Cell[BoxData[ \("Prime"\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Example 4", "Section"], Cell["\<\ The integer 48613 is composite. Can the Miller-Rabin-Selfridge test \ declare it to be prime?\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(PrimeQ[48613]\)], "Input"], Cell[BoxData[ \(False\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MillerRabinSelfridge[48613, 5, Verbose \[Rule] True]\)], "Input"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]35174\), SequenceForm[ "Step 2: ", 35174], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{35174, 9426}\), SequenceForm[ "Step 3: ", {35174, 9426}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]41943\), SequenceForm[ "Step 2: ", 41943], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{41943, 8005}\), SequenceForm[ "Step 3: ", {41943, 8005}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]45604\), SequenceForm[ "Step 2: ", 45604], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{45604, 12063}\), SequenceForm[ "Step 3: ", {45604, 12063}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]33410\), SequenceForm[ "Step 2: ", 33410], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{33410, 25007}\), SequenceForm[ "Step 3: ", {33410, 25007}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]40613\), SequenceForm[ "Step 2: ", 40613], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{40613, 25292}\), SequenceForm[ "Step 3: ", {40613, 25292}], Editable->False]], "Print"], Cell[BoxData[ \({"Composite", {12153, 2}, {11087, 11548, 24240, 6944, 6947}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MillerRabinSelfridge[48613, {17, 19, 23}, Verbose \[Rule] True]\)], "Input"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]4621\), SequenceForm[ "Step 2: ", 4621], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{4621, 12534}\), SequenceForm[ "Step 3: ", {4621, 12534}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]16041\), SequenceForm[ "Step 2: ", 16041], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{16041, 5072}\), SequenceForm[ "Step 3: ", {16041, 5072}], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 2: "\[InvisibleSpace]41023\), SequenceForm[ "Step 2: ", 41023], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[\("Step 3: "\[InvisibleSpace]{41023, 1695}\), SequenceForm[ "Step 3: ", {41023, 1695}], Editable->False]], "Print"], Cell[BoxData[ \({"Composite", {12153, 2}}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Example 5", "Section"], Cell["\<\ Let's see if we can find some composite numbers which pass the \ Miller-Rabin-Selfridge test.\ \>", "Text"], Cell[BoxData[ \(\(facts = Prime[Range[200000, 200099]];\)\)], "Input"], Cell[BoxData[ \(\(n = Union[Flatten[Outer[Times, facts, facts]]];\)\)], "Input"], Cell[BoxData[ \(\(data = Map[MillerRabinSelfridge[#, 1] &, n];\)\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Count[data, "\"]\)], "Input"], Cell[BoxData[ \(5050\)], "Output"] }, Open ]] }, Closed]] }, Open ]] }, FrontEndVersion->"5.0 for X", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{520, 600}, WindowMargins->{{142, Automatic}, {Automatic, 0}} ] (******************************************************************* 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[1754, 51, 54, 1, 27, "Input"], Cell[CellGroupData[{ Cell[1833, 56, 68, 0, 152, "Title"], Cell[1904, 58, 1222, 44, 197, "Text"], Cell[CellGroupData[{ Cell[3151, 106, 28, 0, 72, "Section"], Cell[3182, 108, 102, 3, 32, "Text"], Cell[CellGroupData[{ Cell[3309, 115, 49, 1, 27, "Input"], Cell[3361, 118, 38, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[3436, 124, 99, 2, 43, "Input"], Cell[3538, 128, 43, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[3618, 134, 53, 1, 27, "Input"], Cell[3674, 137, 38, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[3749, 143, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[3804, 147, 64, 1, 27, "Input"], Cell[3871, 150, 38, 1, 27, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[3958, 157, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[4013, 161, 54, 1, 27, "Input"], Cell[4070, 164, 35, 1, 27, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[4154, 171, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[4209, 175, 66, 1, 27, "Input"], Cell[4278, 178, 37, 1, 27, "Output"] }, Open ]], Cell[4330, 182, 234, 8, 50, "Text"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[4613, 196, 28, 0, 42, "Section"], Cell[4644, 198, 104, 3, 32, "Text"], Cell[CellGroupData[{ Cell[4773, 205, 50, 1, 27, "Input"], Cell[4826, 208, 39, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4902, 214, 99, 2, 43, "Input"], Cell[5004, 218, 41, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[5082, 224, 53, 1, 27, "Input"], Cell[5138, 227, 38, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[5213, 233, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[5268, 237, 64, 1, 27, "Input"], Cell[5335, 240, 38, 1, 27, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[5422, 247, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[5477, 251, 54, 1, 27, "Input"], Cell[5534, 254, 39, 1, 27, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[5622, 261, 30, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[5677, 265, 66, 1, 27, "Input"], Cell[5746, 268, 77, 1, 27, "Output"] }, Open ]], Cell[5838, 272, 208, 8, 50, "Text"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[6095, 286, 28, 0, 42, "Section"], Cell[CellGroupData[{ Cell[6148, 290, 84, 1, 27, "Input"], Cell[6235, 293, 145, 3, 23, "Print"], Cell[6383, 298, 149, 3, 23, "Print"], Cell[6535, 303, 139, 3, 23, "Print"], Cell[6677, 308, 145, 3, 23, "Print"], Cell[6825, 313, 149, 3, 23, "Print"], Cell[6977, 318, 139, 3, 23, "Print"], Cell[7119, 323, 145, 3, 23, "Print"], Cell[7267, 328, 149, 3, 23, "Print"], Cell[7419, 333, 84, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[7540, 339, 73, 1, 27, "Input"], Cell[7616, 342, 41, 1, 27, "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[7706, 349, 28, 0, 42, "Section"], Cell[7737, 351, 117, 3, 50, "Text"], Cell[CellGroupData[{ Cell[7879, 358, 46, 1, 27, "Input"], Cell[7928, 361, 39, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8004, 367, 85, 1, 27, "Input"], Cell[8092, 370, 147, 3, 23, "Print"], Cell[8242, 375, 163, 3, 23, "Print"], Cell[8408, 380, 147, 3, 23, "Print"], Cell[8558, 385, 163, 3, 23, "Print"], Cell[8724, 390, 147, 3, 23, "Print"], Cell[8874, 395, 165, 3, 23, "Print"], Cell[9042, 400, 147, 3, 23, "Print"], Cell[9192, 405, 165, 3, 23, "Print"], Cell[9360, 410, 147, 3, 23, "Print"], Cell[9510, 415, 165, 3, 23, "Print"], Cell[9678, 420, 103, 2, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[9818, 427, 103, 2, 43, "Input"], Cell[9924, 431, 145, 3, 23, "Print"], Cell[10072, 436, 163, 3, 23, "Print"], Cell[10238, 441, 147, 3, 23, "Print"], Cell[10388, 446, 163, 3, 23, "Print"], Cell[10554, 451, 147, 3, 23, "Print"], Cell[10704, 456, 163, 3, 23, "Print"], Cell[10870, 461, 59, 1, 27, "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[10978, 468, 28, 0, 42, "Section"], Cell[11009, 470, 117, 3, 50, "Text"], Cell[11129, 475, 74, 1, 27, "Input"], Cell[11206, 478, 84, 1, 27, "Input"], Cell[11293, 481, 81, 1, 27, "Input"], Cell[CellGroupData[{ Cell[11399, 486, 61, 1, 27, "Input"], Cell[11463, 489, 38, 1, 27, "Output"] }, Open ]] }, Closed]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)