(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 8.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[     13298,        439]
NotebookOptionsPosition[      7508,        292]
NotebookOutlinePosition[      8310,        322]
CellTagsIndexPosition[      8209,        316]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["EPM`Collatz`", "Section",
 InitializationGroup->True],

Cell[TextData[{
 StyleBox["From",
  FontWeight->"Bold"],
 ": ",
 StyleBox["Essentials of Programming in Mathematica\n",
  FontSlant->"Italic"],
 "\[Copyright] Paul R. Wellin 2015"
}], "Text",
 CellMargins->{{28, 10}, {7, 7}},
 FontSize->14],

Cell[CellGroupData[{

Cell["Metadata", "Subsection"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", "Name", ":", " ", "EPM`Collatz`"}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", "Book", ":", " ", 
   RowBox[{
   "Essentials", " ", "of", " ", "programming", " ", "in", " ", 
    "Mathematica"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Author", ":", " ", 
    RowBox[{"Paul", " ", "Wellin"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", "Summary", ":", " ", 
   RowBox[{
   "This", " ", "package", " ", "contains", " ", "the", " ", "functions", " ",
     "defined", " ", "in", " ", "the", " ", "solutions", " ", "to", " ", 
    "the", " ", "Collatz", " ", "exercises", " ", "in", " ", "Chapter", " ", 
    "10", " ", 
    RowBox[{"Packages", "."}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", "Context", ":", " ", "EPM`Collatz`"}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Package", " ", 
    RowBox[{"Version", ":", " ", "1.0"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", "Copyright", ":", " ", 
    RowBox[{"Copyright", " ", "2015"}]}], ",", " ", 
   RowBox[{"Paul", " ", "R", " ", "Wellin"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"History", ":", " ", 
     RowBox[{
     "Version", " ", "1.0", " ", "by", " ", "Paul", " ", "Wellin"}]}]}], ",", 
   " ", 
   RowBox[{"Dec", " ", "2012"}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", "Keywords", ":", " ", 
    RowBox[{"Collatz", " ", "sequence"}]}], ",", " ", "PositiveIntegerQ"}], 
  " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Source", ":", " ", 
    RowBox[{"Collatz", ".", "nb"}]}]}], " ", "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Mathematica", " ", "Version"}], ":", " ", "10.3"}], " ", 
  "*)"}]], "Input"],

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{"Tested", " ", "in", " ", "Mathematica", " ", "11.1"}], " ", 
  "*)"}]], "Input",ExpressionUUID->"fa633574-d74b-484c-9b75-8c7fafc03071"]
}, Closed]],

Cell[CellGroupData[{

Cell["BeginPackage", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"Remove", "[", "CollatzSequence", "]"}], ";"}]], "Input"],

Cell[BoxData[
 RowBox[{"BeginPackage", "[", "\"\<EPM`Collatz`\>\"", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Usage statements", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"CollatzSequence", "::", "usage"}], "=", "\"\<CollatzSequence[\!\(\*
StyleBox[\"n\", \"TI\"]\)] computes the sequence of Collatz iterates starting \
with initial value \!\(\*
StyleBox[\"n\", \"TI\"]\). The sequence terminates as soon as it reaches the \
value 1.\>\""}], ";"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["Warning messages", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"CollatzSequence", "::", "notint"}], "=", 
   "\"\<The argument, `1`, to CollatzSequence must be a positive \
integer.\>\""}], ";"}]], "Input"]
}, Open  ]],

Cell["Options", "Subsection"],

Cell[CellGroupData[{

Cell["Begin private context", "Subsection"],

Cell[BoxData[
 RowBox[{"Begin", "[", "\"\<`Private`\>\"", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["The function definitions", "Subsection"],

Cell[BoxData[
 RowBox[{
  RowBox[{"collatz", "[", 
   RowBox[{"n_", "?", "EvenQ"}], "]"}], ":=", 
  RowBox[{"n", "/", "2"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"collatz", "[", 
   RowBox[{"n_", "?", "OddQ"}], "]"}], ":=", 
  RowBox[{
   RowBox[{"3", "n"}], "+", "1"}]}]], "Input"],

Cell[BoxData[
 RowBox[{
  RowBox[{"positiveIntegerQ", "[", "n_", "]"}], ":=", 
  RowBox[{
   RowBox[{"IntegerQ", "[", "n", "]"}], "&&", 
   RowBox[{"Positive", "[", "n", "]"}]}]}]], "Input",ExpressionUUID->\
"00e1c497-362e-4629-a71d-64c49300b8c2"],

Cell[BoxData[
 RowBox[{
  RowBox[{"CollatzSequence", "[", "n_", "]"}], ":=", "\[IndentingNewLine]", 
  RowBox[{"If", "[", 
   RowBox[{
    RowBox[{"positiveIntegerQ", "[", "n", "]"}], ",", "\[IndentingNewLine]", 
    RowBox[{"NestWhileList", "[", 
     RowBox[{"collatz", ",", "n", ",", 
      RowBox[{
       RowBox[{"#", "\[NotEqual]", "1"}], "&"}]}], "]"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"Message", "[", 
     RowBox[{
      RowBox[{"CollatzSequence", "::", "notint"}], ",", "n"}], "]"}]}], 
   "\[IndentingNewLine]", "]"}]}]], "Input",ExpressionUUID->"17e49608-bde1-\
4139-980a-eca664f3aa25"],

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"CollatzSequence", "[", 
    RowBox[{"_", ",", "args__"}], "]"}], "/;", 
   RowBox[{"Message", "[", 
    RowBox[{
     RowBox[{
      StyleBox["CollatzSequence", "Color1"], "::", "argx"}], ",", 
     "CollatzSequence", ",", 
     RowBox[{
      RowBox[{"Length", "[", 
       RowBox[{"{", "args", "}"}], "]"}], "+", "1"}]}], "]"}]}], ":=", 
  "Null"}]], "Input",
 CellTags->{"M220:7.8.10-2", "M220.7.8"}]
}, Closed]],

Cell[CellGroupData[{

Cell["End private context", "Subsection"],

Cell[BoxData[
 RowBox[{"End", "[", "]"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["EndPackage", "Subsection"],

Cell[BoxData[
 RowBox[{"EndPackage", "[", "]"}]], "Input"]
}, Closed]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Examples", "Section"],

Cell[CellGroupData[{

Cell["Load package", "Subsection"],

Cell[BoxData["Quit"], "Input",ExpressionUUID->"2ebe327d-befe-4591-ad6d-52289f51bd72"],

Cell[TextData[{
 "Load package (do ",
 StyleBox["not",
  FontSlant->"Italic"],
 " say \[OpenCurlyDoubleQuote]Yes\[CloseCurlyDoubleQuote] to evaluate init \
cells dialog)."
}], "Text"],

Cell[BoxData[
 RowBox[{"<<", "EPM`Collatz`"}]], "Input",ExpressionUUID->"dd66884e-998a-4f88-91b0-99f97df03bfd"]
}, Open  ]],

Cell[CellGroupData[{

Cell["Test usage messages", "Subsection"],

Cell[BoxData[
 RowBox[{"?", "CollatzSequence"}]], "Input",ExpressionUUID->"7d61ccf0-3ab2-41eb-b920-a2c9b70e22e6"]
}, Closed]],

Cell[CellGroupData[{

Cell["Exercise the functions", "Subsection"],

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", "23", "]"}]], "Input",ExpressionUUID->"4988827a-ee78-4396-bd13-9579b7cfa948"],

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", 
  RowBox[{"-", "5"}], "]"}]], "Input",ExpressionUUID->"a0cb46ba-18e1-4b29-\
90f3-091a98d2c4e9"],

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", 
  RowBox[{"3", ",", "4"}], "]"}]], "Input",ExpressionUUID->"daa64e6b-8e24-\
4699-a8ae-ca6e106ea6b1"]
}, Closed]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{808, 699},
WindowMargins->{{Automatic, 860}, {Automatic, 41}},
ShowSelection->True,
FrontEndVersion->"11.1 for Mac OS X x86 (32-bit, 64-bit Kernel) (March 16, \
2017)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{
 "M220.7.8"->{
  Cell[5546, 209, 450, 14, 
  54, "Input", "ExpressionUUID" -> "0fc49bb5-0643-4444-b15a-4447c289c5ea",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]},
 "M220:7.8.10-2"->{
  Cell[5546, 209, 450, 14, 
  54, "Input", "ExpressionUUID" -> "0fc49bb5-0643-4444-b15a-4447c289c5ea",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]}
 }
*)
(*CellTagsIndex
CellTagsIndex->{
 {"M220.7.8", 7868, 305},
 {"M220:7.8.10-2", 8037, 309}
 }
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[579, 22, 59, 1, 64, "Section", "ExpressionUUID" -> \
"360ed4cd-2c2d-4557-a154-c77c09e97305",
 InitializationGroup->True],
Cell[641, 25, 240, 9, 49, "Text", "ExpressionUUID" -> \
"e7e5d3a0-aeea-4c2a-9bbe-05e898394880"],
Cell[CellGroupData[{
Cell[906, 38, 30, 0, 44, "Subsection", "ExpressionUUID" -> \
"650bb74a-2414-4a08-96c2-8beda6e3419e"],
Cell[939, 40, 108, 2, 32, "Input", "ExpressionUUID" -> \
"daf4a982-b6f6-460f-afbe-55f490ccb7ae"],
Cell[1050, 44, 191, 5, 32, "Input", "ExpressionUUID" -> \
"438eb64f-34ab-4f70-beca-3473d7f8d9f0"],
Cell[1244, 51, 146, 4, 32, "Input", "ExpressionUUID" -> \
"1a9b1d9e-98a0-450d-8908-012532679865"],
Cell[1393, 57, 388, 8, 54, "Input", "ExpressionUUID" -> \
"dbbf4eeb-6997-4ae2-9d8c-5e34b349aab1"],
Cell[1784, 67, 111, 2, 32, "Input", "ExpressionUUID" -> \
"fce999c2-b9d2-41b0-b7ed-a278ed0a3f13"],
Cell[1898, 71, 147, 4, 32, "Input", "ExpressionUUID" -> \
"a9bf5fce-18c7-487e-826a-923322ec3356"],
Cell[2048, 77, 209, 5, 32, "Input", "ExpressionUUID" -> \
"94f0be69-8b69-4adb-ae7a-fc219c25915a"],
Cell[2260, 84, 256, 8, 32, "Input", "ExpressionUUID" -> \
"e53603de-00ab-4b72-a6f6-230b6c30bf66"],
Cell[2519, 94, 186, 5, 32, "Input", "ExpressionUUID" -> \
"50b2cc41-b385-4571-a9f6-28c60f67860f"],
Cell[2708, 101, 145, 4, 32, "Input", "ExpressionUUID" -> \
"3705fab7-1743-4ca3-b683-9d8b458b55e6"],
Cell[2856, 107, 140, 4, 32, "Input", "ExpressionUUID" -> \
"402c4e16-b7be-4357-865f-3c184d8a0511"],
Cell[2999, 113, 181, 3, 32, "Input", "ExpressionUUID" -> \
"fa633574-d74b-484c-9b75-8c7fafc03071"]
}, Closed]],
Cell[CellGroupData[{
Cell[3217, 121, 34, 0, 36, "Subsection", "ExpressionUUID" -> \
"97f31e33-fae4-4888-87cf-e580a670adb3"],
Cell[3254, 123, 91, 2, 32, "Input", "ExpressionUUID" -> \
"bfdee65d-baa7-495f-a4ac-55533646798d"],
Cell[3348, 127, 84, 1, 32, "Input", "ExpressionUUID" -> \
"d9c3dacb-7b5d-45da-9e0b-2b918b463d67"]
}, Closed]],
Cell[CellGroupData[{
Cell[3469, 133, 38, 0, 36, "Subsection", "ExpressionUUID" -> \
"13870ca4-e1cf-4afe-9a1c-18ec09c73321"],
Cell[3510, 135, 333, 7, 96, "Input", "ExpressionUUID" -> \
"f81a686a-498c-411d-ab3e-012ae7f38bad"]
}, Closed]],
Cell[CellGroupData[{
Cell[3880, 147, 38, 0, 36, "Subsection", "ExpressionUUID" -> \
"d495240f-67d3-4b8c-a3df-567a541684e7"],
Cell[3921, 149, 189, 5, 54, "Input", "ExpressionUUID" -> \
"13765d8c-9bdb-4230-91ad-d6d8cc23278b"]
}, Open  ]],
Cell[4125, 157, 29, 0, 44, "Subsection", "ExpressionUUID" -> \
"6c395fff-fc17-4b98-a6c1-44dab8408c17"],
Cell[CellGroupData[{
Cell[4179, 161, 43, 0, 36, "Subsection", "ExpressionUUID" -> \
"a78f9620-65c2-43ce-ad34-6d37e6538968"],
Cell[4225, 163, 74, 1, 32, "Input", "ExpressionUUID" -> \
"bdcd3173-68f7-4780-a0fa-622656c49ae4"]
}, Closed]],
Cell[CellGroupData[{
Cell[4336, 169, 46, 0, 36, "Subsection", "ExpressionUUID" -> \
"ef70a821-f09b-4287-a1bc-244ceb968180"],
Cell[4385, 171, 136, 4, 32, "Input", "ExpressionUUID" -> \
"7afb98cf-140d-4f81-86a8-0105674a64fa"],
Cell[4524, 177, 154, 5, 32, "Input", "ExpressionUUID" -> \
"571f5931-20a6-4bc0-bf2f-153b78466a4a"],
Cell[4681, 184, 247, 6, 32, "Input", "ExpressionUUID" -> \
"00e1c497-362e-4629-a71d-64c49300b8c2"],
Cell[4931, 192, 612, 15, 117, "Input", "ExpressionUUID" -> \
"17e49608-bde1-4139-980a-eca664f3aa25"],
Cell[5546, 209, 450, 14, 54, "Input", "ExpressionUUID" -> \
"0fc49bb5-0643-4444-b15a-4447c289c5ea",
 CellTags->{"M220:7.8.10-2", "M220.7.8"}]
}, Closed]],
Cell[CellGroupData[{
Cell[6033, 228, 41, 0, 36, "Subsection", "ExpressionUUID" -> \
"4699e47c-4b9b-4311-8259-f27c4c23bd83"],
Cell[6077, 230, 51, 1, 28, "Input", "ExpressionUUID" -> \
"5c9a0b2a-a825-40d0-93ff-bdd5844b7cbd"]
}, Closed]],
Cell[CellGroupData[{
Cell[6165, 236, 32, 0, 36, "Subsection", "ExpressionUUID" -> \
"65725ed2-81ac-4f83-9888-b067a4866e21"],
Cell[6200, 238, 58, 1, 32, "Input", "ExpressionUUID" -> \
"a4c1c9ac-4537-47a4-a8e8-61ed3e10359a"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[6307, 245, 27, 0, 64, "Section", "ExpressionUUID" -> \
"5a5c4774-199b-45f3-b8a9-b430d4042297"],
Cell[CellGroupData[{
Cell[6359, 249, 34, 0, 44, "Subsection", "ExpressionUUID" -> \
"6a8b685b-09fc-4ae1-a4e0-d1d57fca2a5f"],
Cell[6396, 251, 85, 0, 32, "Input", "ExpressionUUID" -> \
"2ebe327d-befe-4591-ad6d-52289f51bd72"],
Cell[6484, 253, 183, 6, 30, "Text", "ExpressionUUID" -> \
"ff4ca68a-16dc-414e-a905-05fe1ab721f9"],
Cell[6670, 261, 111, 1, 32, "Input", "ExpressionUUID" -> \
"dd66884e-998a-4f88-91b0-99f97df03bfd"]
}, Open  ]],
Cell[CellGroupData[{
Cell[6818, 267, 41, 0, 44, "Subsection", "ExpressionUUID" -> \
"1841961f-7867-41c3-afd9-dc0426ed2bb3"],
Cell[6862, 269, 113, 1, 32, "Input", "ExpressionUUID" -> \
"7d61ccf0-3ab2-41eb-b920-a2c9b70e22e6"]
}, Closed]],
Cell[CellGroupData[{
Cell[7012, 275, 44, 0, 36, "Subsection", "ExpressionUUID" -> \
"d7b7d8a5-efcc-43a7-bfdb-671c03ce1131"],
Cell[7059, 277, 124, 1, 32, "Input", "ExpressionUUID" -> \
"4988827a-ee78-4396-bd13-9579b7cfa948"],
Cell[7186, 280, 143, 3, 32, "Input", "ExpressionUUID" -> \
"a0cb46ba-18e1-4b29-90f3-091a98d2c4e9"],
Cell[7332, 285, 148, 3, 32, "Input", "ExpressionUUID" -> \
"daa64e6b-8e24-4699-a8ae-ca6e106ea6b1"]
}, Closed]]
}, Open  ]]
}
]
*)

