COLLATZ is a Mathematica package which evaluates the Collatz sequence for a given starting point.
The most interesting thing about COLLATZ is not the Collatz sequence, but rather the way that it demonstrates how a user can define new functions for Mathematica, to be stored in a package file, and accessed later by, for example, a notebook.
The Collatz sequence itself, sometimes called the "3N+1" sequence, is defined as follows. Choose a starting value n, a positive integer. Now compute the next value of n:
However, we are much more interested in the steps necessary to make it possible for a user to be able to start up Mathematica, and type
Collatz[17]and see the appropriate sequence of values.
A package is a group of Mathematica function definitions. Packages have a specific format; for instance, they are delimited with BeginPackage[] and EndPackage[] statements. Moreover, (and I forget this every time!) the cells in a package must be explicitly designated as "Initialization Cells" or nothing in the package will actually be usable.
In order for a package to be accessible, it must be placed in a specific directory where Mathematica can find it. Generally, this is the Applications subdirectory of the user base directory, which can be determined by:
$UserBaseDirectory
Once the new package has been placed, you should probably run Mathematica and direct it to rebuild the Help database. This will serve to update Mathematica's internal information so that it can explain how the functions in the package are called.
To use the Collatz function, the user must first load the package, with the command
<< Collatz.mafter which, for the duration of the Mathematica session, the user can invoke the Collatz function with commands like
Collatz[99]
The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.
COLLATZ is available in a JAVA version and a MATHEMATICA version and a MATLAB version and a PERL version.
POLPAK is a FORTRAN90 library which includes the routine collatz_count for counting the length of a Collatz sequence.
SEQUENCE_STREAK is a MATLAB program which can be used to make a "streak plot" of a Collatz sequence for a particular starting value, or for the length of the Collatz sequence for a number of starting values.
You can go up one level to the Mathematica packages and notebooks.