(* 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[     11709,        447]
NotebookOptionsPosition[      8492,        333]
NotebookOutlinePosition[      9320,        365]
CellTagsIndexPosition[      9179,        358]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

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

Cell[TextData[{
 StyleBox["From",
  FontWeight->"Bold"],
 ": ",
 StyleBox[ButtonBox["Programming with Mathematica: An Introduction",
  BaseStyle->"Hyperlink",
  ButtonData->{
    URL["http://www.cambridge.org/wellin"], None},
  ButtonNote->"http://www.cambridge.org/wellin"],
  FontSlant->"Italic",
  FontVariations->{"Underline"->True}]
}], "Text",
 CellMargins->{{28, 10}, {7, 7}},
 FontSize->14],

Cell[CellGroupData[{

Cell["Metadata", "Subsection"],

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

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Book", ":", " ", 
    RowBox[{"Programming", " ", "with", " ", 
     RowBox[{"Mathematica", ":", " ", 
      RowBox[{"An", " ", "Introduction"}]}]}]}]}], " ", "*)"}]], "Input"],

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

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

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

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

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{
   RowBox[{":", 
    RowBox[{"Copyright", ":", " ", 
     RowBox[{"Copyright", " ", "2012"}]}]}], ",", " ", 
   RowBox[{"Cambridge", " ", "University", " ", "Press"}]}], " ", 
  "*)"}]], "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"}]}]}], " ", "*)"}]], "Input"],

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

Cell[BoxData[
 RowBox[{"(*", " ", 
  RowBox[{":", 
   RowBox[{"Mathematica", " ", 
    RowBox[{"Version", ":", " ", 
     RowBox[{"8.0", ".4"}]}]}]}], " ", "*)"}]], "Input"]
}, Closed]],

Cell[CellGroupData[{

Cell["BeginPackage", "Subsection"],

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

Cell[CellGroupData[{

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

Cell[BoxData[
 RowBox[{
  RowBox[{
   RowBox[{"CollatzSequence", "::", "usage"}], "=", 
   "\"\<CollatzSequence[n] computes the sequence of Collatz iterates starting \
with initial value n. 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"}], "=", 
   "\"\<First argument, `1`, to CollatzSequence must be a positive integer.\>\
\""}], ";"}]], "Input"]
}, Closed]],

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[{"CollatzSequence", "[", "n_", "]"}], ":=", "\[IndentingNewLine]", 
  RowBox[{"If", "[", 
   RowBox[{
    RowBox[{
     RowBox[{"IntegerQ", "[", "n", "]"}], "&&", 
     RowBox[{"n", "\[GreaterEqual]", "0"}]}], ",", "\[IndentingNewLine]", 
    RowBox[{"NestWhileList", "[", 
     RowBox[{"collatz", ",", "n", ",", 
      RowBox[{
       RowBox[{"#", "\[NotEqual]", "1"}], "&"}]}], "]"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"Message", "[", 
     RowBox[{
      RowBox[{"CollatzSequence", "::", "notint"}], ",", "n"}], "]"}]}], 
   "\[IndentingNewLine]", "]"}]}]], "Input"],

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"],

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

Cell[BoxData[
 RowBox[{"<<", "PWM`Collatz`"}]], "Input"]
}, Open  ]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"?", "CollatzSequence"}]], "Input"],

Cell[BoxData[
 StyleBox["\<\"CollatzSequence[\!\(\*\\nStyleBox[\\\"n\\\", \\\"TI\\\"]\)] \
computes the sequence of Collatz iterates starting with initial value \
\!\(\*\\nStyleBox[\\\"n\\\", \\\"TI\\\"]\). The sequence terminates as soon \
as it reaches the value 1.\"\>", "MSG"]], "Print", "PrintUsage",
 CellTags->"Info3552387181-6620503"]
}, Closed]]
}, Closed]],

Cell[CellGroupData[{

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

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", "23", "]"}]], "Input"],

Cell[BoxData[
 RowBox[{"{", 
  RowBox[{
  "23", ",", "70", ",", "35", ",", "106", ",", "53", ",", "160", ",", "80", 
   ",", "40", ",", "20", ",", "10", ",", "5", ",", "16", ",", "8", ",", "4", 
   ",", "2", ",", "1"}], "}"}]], "Output"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", 
  RowBox[{"-", "5"}], "]"}]], "Input"],

Cell[BoxData[
 RowBox[{
  StyleBox[
   RowBox[{"CollatzSequence", "::", "notint"}], "MessageName"], ":", 
  " ", "\<\"First argument, \[NoBreak]\\!\\(-5\\)\[NoBreak], to \
CollatzSequence must be a positive integer.\"\>"}]], "Message", "MSG"]
}, Closed]],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", 
  RowBox[{"3", ",", "4"}], "]"}]], "Input"],

Cell[BoxData[
 RowBox[{
  StyleBox[
   RowBox[{"CollatzSequence", "::", "argx"}], "MessageName"], ":", 
  " ", "\<\"\[NoBreak]\\!\\(CollatzSequence\\)\[NoBreak] called with \
\[NoBreak]\\!\\(2\\)\[NoBreak] arguments; 1 argument is expected. \
\\!\\(\\*ButtonBox[\\\"\[RightSkeleton]\\\", ButtonStyle->\\\"Link\\\", \
ButtonFrame->None, ButtonData:>\\\"paclet:ref/message/General/argx\\\", \
ButtonNote -> \\\"PWM`Collatz`CollatzSequence::argx\\\"]\\)\"\>"}]], "Message",\
 "MSG"],

Cell[BoxData[
 RowBox[{"CollatzSequence", "[", 
  RowBox[{"3", ",", "4"}], "]"}]], "Output"]
}, Closed]]
}, Closed]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{865, 801},
WindowMargins->{{Automatic, 496}, {Automatic, 32}},
ShowSelection->True,
FrontEndVersion->"10.1 for Mac OS X x86 (32-bit, 64-bit Kernel) (March 23, \
2015)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{
 "M220:7.8.10-2"->{
  Cell[5321, 204, 450, 14, 175, "Input",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]},
 "M220.7.8"->{
  Cell[5321, 204, 450, 14, 175, "Input",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]},
 "Info3552387181-6620503"->{
  Cell[6610, 269, 342, 5, 168, "Print",
   CellTags->"Info3552387181-6620503"]}
 }
*)
(*CellTagsIndex
CellTagsIndex->{
 {"M220:7.8.10-2", 8857, 346},
 {"M220.7.8", 8959, 349},
 {"Info3552387181-6620503", 9075, 352}
 }
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[579, 22, 59, 1, 60, "Section",
 InitializationGroup->True],
Cell[641, 25, 398, 13, 29, "Text"],
Cell[CellGroupData[{
Cell[1064, 42, 30, 0, 41, "Subsection"],
Cell[1097, 44, 122, 3, 33, "Input"],
Cell[1222, 49, 239, 6, 33, "Input"],
Cell[1464, 57, 146, 4, 33, "Input"],
Cell[1613, 63, 479, 11, 72, "Input"],
Cell[2095, 76, 125, 3, 33, "Input"],
Cell[2223, 81, 147, 4, 33, "Input"],
Cell[2373, 87, 241, 7, 33, "Input"],
Cell[2617, 96, 256, 8, 33, "Input"],
Cell[2876, 106, 153, 4, 33, "Input"],
Cell[3032, 112, 145, 4, 33, "Input"],
Cell[3180, 118, 173, 5, 33, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3390, 128, 34, 0, 33, "Subsection"],
Cell[3427, 130, 84, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3548, 136, 38, 0, 33, "Subsection"],
Cell[3589, 138, 275, 6, 63, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3901, 149, 38, 0, 33, "Subsection"],
Cell[3942, 151, 191, 5, 46, "Input"]
}, Closed]],
Cell[4148, 159, 29, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[4202, 163, 43, 0, 33, "Subsection"],
Cell[4248, 165, 74, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4359, 171, 46, 0, 33, "Subsection"],
Cell[4408, 173, 136, 4, 78, "Input"],
Cell[4547, 179, 154, 5, 78, "Input"],
Cell[4704, 186, 614, 16, 266, "Input"],
Cell[5321, 204, 450, 14, 175, "Input",
 CellTags->{"M220:7.8.10-2", "M220.7.8"}]
}, Closed]],
Cell[CellGroupData[{
Cell[5808, 223, 41, 0, 33, "Subsection"],
Cell[5852, 225, 51, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[5940, 231, 32, 0, 33, "Subsection"],
Cell[5975, 233, 58, 1, 78, "Input"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[6082, 240, 27, 0, 60, "Section"],
Cell[CellGroupData[{
Cell[6134, 244, 34, 0, 41, "Subsection"],
Cell[6171, 246, 30, 0, 28, "Input"],
Cell[6204, 248, 183, 6, 29, "Text"],
Cell[6390, 256, 56, 1, 28, "Input"]
}, Open  ]],
Cell[CellGroupData[{
Cell[6483, 262, 41, 0, 41, "Subsection"],
Cell[CellGroupData[{
Cell[6549, 266, 58, 1, 78, "Input"],
Cell[6610, 269, 342, 5, 168, "Print",
 CellTags->"Info3552387181-6620503"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[7001, 280, 44, 0, 33, "Subsection"],
Cell[CellGroupData[{
Cell[7070, 284, 69, 1, 28, "Input"],
Cell[7142, 287, 237, 5, 78, "Output"]
}, Closed]],
Cell[CellGroupData[{
Cell[7416, 297, 86, 2, 22, "Input"],
Cell[7505, 301, 242, 5, 62, "Message"]
}, Closed]],
Cell[CellGroupData[{
Cell[7784, 311, 91, 2, 22, "Input"],
Cell[7878, 315, 479, 9, 62, "Message"],
Cell[8360, 326, 92, 2, 78, "Output"]
}, Closed]]
}, Closed]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
