COLLATZ
A Simple Package for the Collatz Sequence


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:

For every starting value of n, the sequence seems to terminate, and it is conjectured that this is always so.

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.

Package Installation:

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.

Package Usage:

To use the Collatz function, the user must first load the package, with the command

        << Collatz.m
      
after which, for the duration of the Mathematica session, the user can invoke the Collatz function with commands like
        Collatz[99]
      

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

COLLATZ is available in a JAVA version and a MATHEMATICA version and a MATLAB version and a PERL version.

Related Data and Programs:

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.

Reference:

  1. Roman Maeder,
    Programming in Mathematica,
    Second Edition, Addison Wesley, 1991.
  2. Eric Weisstein,
    "The Collatz Problem",
    CRC Concise Encyclopedia of Mathematics,
    CRC 1998.
  3. Stephen Wolfram,
    The Mathematica Book,
    Fourth Edition,
    Wolfram Media / Cambridge University Press, 1999.

Source Code:

Examples and Tests:

List of Routines:

You can go up one level to the Mathematica packages and notebooks.


Last revised on 05 March 2006.