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.
In order for the package to be accessible, the package file must be placed in the appropriate place. A typical place is the "Applications" subdirectory of the "User Base Directory". These are directories set up by Mathematica. You can find out where the User Base Directory is by the Mathematica command:
$UserBaseDirectory
Then you need to copy the package file to the "Applications"
subdirectory of that directory.
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.m
after which, for the duration of the Mathematica session, the
user can invoke the Collatz function with commands like
Collatz[99]
COLLATZ is also available in a JAVA version and a MATLAB version and a PERL version.
POLPAK includes the routine collatz_count for counting the length of a Collatz sequence.
You can go up one level to the Mathematica packages and notebooks.