(* 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[     11897,        452]
NotebookOptionsPosition[      8576,        336]
NotebookOutlinePosition[      9505,        370]
CellTagsIndexPosition[      9364,        363]
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[\!\(\*
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"}], "=", 
   "\"\<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"]
}, Closed]],

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"], 
  RowBox[{
  ":", " "}], "\<\"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"], 
  RowBox[{
  ":", " "}], "\<\"\[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]]
}, Open  ]]
}, Open  ]]
},
AutoGeneratedPackage->Automatic,
WindowSize->{865, 801},
WindowMargins->{{Automatic, 496}, {Automatic, 32}},
ShowSelection->True,
Magnification:>FEPrivate`If[
  FEPrivate`Equal[FEPrivate`$VersionNumber, 6.], 1.25, 1.25 Inherited],
FrontEndVersion->"8.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (October 5, \
2011)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{
 "M220:7.8.10-2"->{
  Cell[5379, 205, 450, 14, 175, "Input",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]},
 "M220.7.8"->{
  Cell[5379, 205, 450, 14, 175, "Input",
   CellTags->{"M220:7.8.10-2", "M220.7.8"}]},
 "Info3552387181-6620503"->{
  Cell[6668, 270, 342, 5, 168, "Print",
   CellTags->"Info3552387181-6620503"]}
 }
*)
(*CellTagsIndex
CellTagsIndex->{
 {"M220:7.8.10-2", 9042, 351},
 {"M220.7.8", 9144, 354},
 {"Info3552387181-6620503", 9260, 357}
 }
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[579, 22, 59, 1, 83, "Section",
 InitializationGroup->True],
Cell[641, 25, 398, 13, 34, "Text"],
Cell[CellGroupData[{
Cell[1064, 42, 30, 0, 42, "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, 31, "Subsection"],
Cell[3427, 130, 84, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3548, 136, 38, 0, 31, "Subsection"],
Cell[3589, 138, 333, 7, 113, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[3959, 150, 38, 0, 31, "Subsection"],
Cell[4000, 152, 191, 5, 127, "Input"]
}, Closed]],
Cell[4206, 160, 29, 0, 31, "Subsection"],
Cell[CellGroupData[{
Cell[4260, 164, 43, 0, 31, "Subsection"],
Cell[4306, 166, 74, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[4417, 172, 46, 0, 31, "Subsection"],
Cell[4466, 174, 136, 4, 78, "Input"],
Cell[4605, 180, 154, 5, 78, "Input"],
Cell[4762, 187, 614, 16, 266, "Input"],
Cell[5379, 205, 450, 14, 175, "Input",
 CellTags->{"M220:7.8.10-2", "M220.7.8"}]
}, Closed]],
Cell[CellGroupData[{
Cell[5866, 224, 41, 0, 31, "Subsection"],
Cell[5910, 226, 51, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[5998, 232, 32, 0, 31, "Subsection"],
Cell[6033, 234, 58, 1, 78, "Input"]
}, Closed]]
}, Open  ]],
Cell[CellGroupData[{
Cell[6140, 241, 27, 0, 83, "Section"],
Cell[CellGroupData[{
Cell[6192, 245, 34, 0, 42, "Subsection"],
Cell[6229, 247, 30, 0, 78, "Input"],
Cell[6262, 249, 183, 6, 77, "Text"],
Cell[6448, 257, 56, 1, 78, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[6541, 263, 41, 0, 31, "Subsection"],
Cell[CellGroupData[{
Cell[6607, 267, 58, 1, 78, "Input"],
Cell[6668, 270, 342, 5, 168, "Print",
 CellTags->"Info3552387181-6620503"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[7059, 281, 44, 0, 31, "Subsection"],
Cell[CellGroupData[{
Cell[7128, 285, 69, 1, 33, "Input"],
Cell[7200, 288, 237, 5, 78, "Output"]
}, Closed]],
Cell[CellGroupData[{
Cell[7474, 298, 86, 2, 25, "Input"],
Cell[7563, 302, 255, 6, 62, "Message"]
}, Closed]],
Cell[CellGroupData[{
Cell[7855, 313, 91, 2, 25, "Input"],
Cell[7949, 317, 492, 10, 62, "Message"],
Cell[8444, 329, 92, 2, 78, "Output"]
}, Closed]]
}, Open  ]]
}, Open  ]]
}
]
*)

(* End of internal cache information *)
