The mers package is now available on Sourceforge and issues can be reported there as well as directly to me by email as noted below. The primary programming languages used are C/C++ and Perl. Some of the data that I have about the factorization of Mersenne numbers is also available there, but mostly limited to Mersennes with prime exponents; I have a lot more data related to Mersennes with composite exponents, which simply doesn't fit in the space I have at Sourceforge. The primary script of this package is doWork.pl, which runs various scripts to try to find factors of Mersenne numbers using ECM, P-1, P+1, and trial factoring. To get trial factoring tasks from GIMPS (www.mersenne.org), your username and password for the site need to be in a file named either '.gimps' or 'gimps.txt', which doWork.pl passes to the askGIMPS.pl script with the -f option. The file should have two lines like:
-u username
-p password
If you have a GPU, the doGPU.pl script can be used by doWork.pl to run mfaktc or mfakto, but only versions of those programs that I have modified to specify tasks on the command line rather than in a worktodo.ini file. I am currently trying to get my changes to mfaktc and mfakto accepted by their developers, but that's been a very slow process for many reasons. Without a GPU, the mersfacgmp program will be used, which is likely too slow for current GIMPS trial factoring tasks to help much. The primary purpose of this package is for my own use, to try to factor smaller Mersenne numbers, rather than help GIMPS find new Mersenne primes. The primary program for that is the Linux 'ecm' program, which can do the P-1 and P+1 methods, including creating save files for later runs, as well as ECM factoring. The other essential script for my own use is now mersServer.pl, added in 2020, which tracks the status of the factoring efforts and accepts connections from doWork.pl on other computers to ask for tasks and report results. Unfortunately, I do not currently have an IP address that can be seen on the Internet as a whole, so I can't provide access to my server but intend to do so in the future, perhaps thru Sourceforge. The installation section below is now incorrect in that:
% make install
... is now supported and will install the programs and scripts in the parent directory ('..') of the current directory.

Further Content

The following is mostly out of date but is the only documentation for most of the programs that do the actual number crunching. In particular, git is now used to track changes to the software so version numbers like '8.1' are no longer used. On Ubuntu Linux, install the apt package gmp-ecm to get the ecm (GMP-ECM) program now used for ECM, P-1, and P+1 factoring. I have dropped several programs from the package in June 2020 as obsolete. In some cases, I did so because the freelip library itself is obsolete; libgmp has overtaken it in almost all ways and the ECM support in freelip has been overtaken by the ecm (GMP-ECM) program that uses libgmp. So ecm3, ecmfactor, merspm1, merskfac, mers3p, and the freelip variants are no longer included. I have made quick edits thru the rest of this file based on dropping these programs.

Extremely Brief Instructions For Lucas-Lehmer Testing

First, note that none of the programs for LL testing in the mers package have anything like the speed of George Woltman's prime95; see the GIMPS web site for that. Second, note that I strongly suggest that you read this entire file at least once; it is the closest thing there is to a FAQ for the package. Also, please suggest improvements to this text and feel free to ask questions you do not feel are answered here; that is the primary source of things to add to the next release.

If you want to try to factor Mersenne numbers, you will have to make do with the full instructions below; there are too many possibilities and a couple of other libraries that are likely needed.

Further, you should read the top of the makefile (or Makefile.nofac, if you use it); there are several kinds of computers that will run the programs faster if they are compiled slightly differently as noted in the makefile.

But, if all you want to do is look for Mersenne Primes using the Lucas-Lehmer test, are willing to wait years, perhaps, for a single result, you have a list of exponents to test from the Manual Testing web page and you have unpacked the tar or zip file that the mers package comes in, then it's likely that all you need to do is:

% make -f Makefile.nofac
and wait for it to compile and test the Lucas-Lehmer testers (fftlucas, mersenne1, mersenne2, and MacLucasUNIX).

Reporting "make" errors

If any "make" of the mers package fails at any point, please send me the output. The easiest way to do that is usually to re-direct make's output into a file with something like:

% make >& make.output
(give any other arguments to make that you want right after "make"). If that complains about a "null redirect" or similar, then you're using a Bourne (sh) or compatible shell and it wants something like:

% make > make.output 2>&1
to do the same thing (put stdout and stderr in the same file). Then, after the make completes, email me the file. On most UNIX machines that have email to the Internet:

% mail wedgingt@acm.org < make.output
will do it. The contents of make.output may not have everything I need to solve the problems, but it should tell me what questions to ask you to get the info I need from your computer.

Introduction

The "mers" package is a set of portable C (ANSI or K&R) programs and several scripts for performing Lucas-Lehmer tests and trial, Elliptic Curve Method (ECM), P-1, and P+1 factoring of Mersenne numbers (2^p - 1, where p is usually prime but not always). The C programs are stand alone but some of them can also be used with the scripts and/or TCP/IP to coordinate multiple CPUs thru some central server. The package is maintained by me, Will Edgington (email addresses are at the end of this file), on behalf of the Mersenne mailing list at mersenne@base.com and the "Great Internet Mersenne Prime Search" or Mersenne Prime Project and incorporates ideas, code, and bug fixes from numerous members of both groups.

Since I presently only have access to Ubuntu Linux and MSWindows10 with Cygwin for testing this package, I have to rely on others to test the package on other platforms and send me back changes, guesses at things to try, or at least the error messages. Further, note that George Woltman's programs run much faster on nearly all Intel platforms, including Intel Linux, FreeBSD, NT, Microsoft Windows, etc. Further, John Sweeney has taken many of the ideas from George's code and produced a C language version for Macintosh PowerPC systems (the binary of which is also on George's web page just above); it too appears to be faster than this package's programs on the same hardware. With help from several other people, I have ported John Sweeney's code back to UNIX and it seems to run correctly on several UNIX variants, including Linux, SunOS, SGIs, and IBMs with PowerPC CPUs, so it is now included in this package as MacLucasUNIX. Also, Ernst Mayer has written a Fortran90 program for doing Lucas-Lehmer tests, so, if you have a Fortran90 compiler, that's probably going to be faster than mersenne1 - but perhaps not MacLucasUNIX - of this package. See the mers project on Sourceforge for more and links to others with much more information on these other programs and how to get them.

The other README files (README.fftlucas and README.mersenne1) were originally comments at the top of their respective .c files; they are now very out-of-date and are only retained to show each program's origins, references, and so on. In fact, none of the usage information that was in those files in prior releases was accurate; it's now been deleted entirely to prevent further confusion.

None of the "guts" (calculations-related code) of these programs (with the exception of extract) originated with me; my changes have all been to improve usability and portability, to take advantage of "long double" or "long long" if they're available, define the details of the checkpoint formats, and similar things.

Brief Program Descriptions

There are presently numerous C programs and several scripts. Many of the C programs use rw.c's input() function, which handles command line options as well as reading several input formats: "bare" Mersenne exponents, the "exponent,extent" (power of 2 extent of prior trial factoring efforts) format output by "extract -2" from George Woltman's DATABASE, DATABASE files themselves if named "DATABASE" or "database", the Cunningham Project's "exponent factor.factor. Cdigits" (tabs, not spaces) format, and my "M( exponent )U: extent" and related formats . Input() can handle exponents up to about 2^63 if your compiler directly supports 8 byte integers; most of the programs are limited to 2^64/24 (or 2^32/24) or so, however.

Almost all of the programs of the package that do extensive calculations call setup.c's setup() function, which reduces the priority as low as I know how to lower it so that other programs are always preferred by the CPU. Setup() also sets some signal handlers so that a SIGTERM (terminate, usually 15; sent by the usual UNIX shutdown command), a SIGINT (interrupt, usually 2; often control-C), or a SIGHUP (hangup, usually 1; often sent by logging out) tells the program to save what it's doing and exit as soon as possible.

fftlucas

About the speed of mersenne2, based directly on Crandall and Fagin's lucas.c, with changes by me including the new name, and without the current bug of mersenne1 regarding human readable checkpoint files. Mersenne1 is usually faster than fftlucas by about 15-20% on the same hardware and operating system. Fftlucas uses rw.c's input().

mersenne1

Based on Crandall and Fagin's lucas.c with many changes by Jason Kline, modified by me to use the same command line options and checkpoint file formats as fftlucas, but, due to a bug involving human-readable checkpoint files, they are no longer supported. Mersenne1 uses rw.c's input(). MacLucasUNIX is faster on most computers.

mersenne2

Somewhat slower but easier to follow algorithm with the same authors as mersenne1 and the same lack of human-readable checkpoint support. Mersenne2 uses rw.c's input().

MacLucasUNIX

This Lucas-Lehmer tester is usually about twice the speed of all but one of the other Lucas-Lehmer programs provided here and is based on code from John Sweeney and numerous others; this version also uses rw.c's input() and thus provides the same checkpointing as mersenne1, to the extent that checkpoint files produced by mersenne1 appear to be read and handled correctly by MacLucasUNIX. Reported to be significantly faster than Ernst Mayer's LL program on SGIs, somewhat faster on PowerPC CPUs, and about the same to a bit slower on other CPUs. About a factor of ten slower than George Woltman's LL programs on Intel CPUs.

MacLucasFFTW and tunefftw

This is likely the fastest of the Lucas-Lehmer programs provided. It is based on MacLucasUNIX but was changed by Guillermo Ballester Valor to use the FFTW package from http://www.fftw.org, now often available as a package for most Linux distributions/versions. To compile it, you will need to have FFTW as well; the Ubuntu name of the apt package is fftw-dev. See MacLucasFFTW.c and tunefftw.c for more info.

mersfacgmp

A trial factorer, original author anonymous, with Free Software Foundation (FSF) libgmp.a arbitrary precision library support and changes by me to use rw.c's input(). libgmp.a source code is likely at ftp.gnu.ai.mit.edu and numerous FSF/GNU mirror sites, including UUNET . Mersfacgmp can trial factor prime exponent Mersennes arbitrarily far. Data for even exponent Mersennes will be skipped, but trial factoring data for odd composite exponent Mersenne numbers usually cause the program to crash.

The libgmp library is also now available as a package for most Linux distributions.

sprpgmp

This program uses the GMP library to test the primality of Mersenne cofactors. Mersenne cofactors are the values that remain after a Mersenne number is divided by all known factors, including algebraic factors for composite exponent Mersennes. See the initial comments of sprp.c for other details, usage, etc.

Algebraic Factors

None of the programs will print every prime factor of composite exponent Mersenne numbers because of several possible algebraic factorizations. First:

GCD(2^a - 1, 2^b - 1) = 2^GCD(a, b) - 1
For example, a prime exponent Mersenne number is a factor of all Mersenne numbers whose exponent is a multiple of that prime. Second:

M(2*n) = 2^(2*n) - 1 = (2^n)^2 - 1^2 = (2^n - 1)*(2^n + 1)
       = M(n)*(M(n) + 2)
So the mers package programs will thus only print factors of M(n) + 2 for M(2*n). Third:

M(4*n - 2) + 2 = 2^(4*n - 2) + 1 = (2^(2*n - 1) - 2^n + 1)
				  *(2^(2*n - 1) + 2^n + 1)
So L and M are defined as:
L = 2^(2*n - 1) - 2^n + 1
M = 2^(2*n - 1) + 2^n + 1
and the programs and scripts try to factor both of them whenever it is given a Mersenne exponent that is an odd multiple of 4.

The Mersenne.pm Perl module pulls all of these "predictable" factors out before trying factor what remains. See the description of the "mers format" for more details and my mersenne web page for some related proofs.

extract

Prints George Woltman's DATABASE file, which I believe he has not created for years, in human-readable form or reads the "header" of a checkpoint file (binary or printable ASCII) that was created by fftlucas, mersenne1, mersenne2, MacLucasUNIX, or George Woltman's programs. Prints whether a binary checkpoint file has byte-swapped longs. Accepts several options to limit the list printed from the DATABASE file. A brief message explaining the options can be printed with "extract -v" (yes, I should make all of them consistent). Extract will try to predict when an LL test will complete based on the last modify times of two checkpoint files for the same exponent if those files are given next to each other on the command line. Extract can also often read the "headers" of the checkpoint files created by George Woltman's prime95, mprime, etc., whether those checkpoints are for LL testing or factoring. It does not use rw.c's input().

readDB

Like extract, but reads the newer, but still likely obsolete, DATABASE files. Extract was created for an older format that I still use for other purposes unrelated to DATABASE files, mostly P-1 save files from George's mprime program.

contract

This program, based on code from Jean-Charles Meyrignac, reads the "M( exponent )U: extent" format and produces a DATABASE file in the form expected by older versions of George Woltman's prime95 and other programs. Contract does not use rw.c's input() but does accept a few command line options for the files to read (default stdin) and the filename to create (default "DATABASE") which cannot already exist.

mmtrial

Mmtrial, based on code from Rob Hooft using the freelip library, accepts a Mersenne prime's exponent, p, and the prior extent of factoring, k, on the command line and tries to factor M(M(p)) via trial factoring starting at 2*k*M(p) + 1. See the MMPstats.txt file for more information, including the extent of trial factor testing already performed. It does not use rw.c's input().

mersdistrib

This program is only slightly changed from Peter-Lawrence Montgomery's message to mersenne@base.com in Sep. of 1996 that displays some information on the distribution of Mersenne prime exponents. The current list of Mersenne prime exponents is in the file primeM.txt , which is included with this package. Mersdistrib does not use rw.c's input(); it will only read exponents by themselves from either stdin or a (single) file named on the command line.

1strs8gmp

This program uses my "reverse method" to calculate the exponent of the smallest Mersenne number that a prime number divides evenly. It is unlikely to be of much use to anyone else; I include it mostly for completeness.

isprimegmp

My quick implementation of a polynomial time method of proving primality discovered in the early 2000s. Very slow; ECPP is much more practical despite taking exponential time due to the different constants involved. Unlikely to be of use to anyone (including me); I wrote it out of interest in the algorithm rather than expecting it to be useful.

Compiling

In the Makefile, the locations of libgmp.a and FFTW will likely have to be edited based on where each is installed on your system. The only simple alternative to having libgmp is to not compile the factoring programs by:

% mv -i Makefile Makefile.fac
% mv -i Makefile.nofac Makefile
... before running "make" or using:
% make -f Makefile.nofac
... instead of just 'make'.

Mersfacint does not require libgmp.a, but Makefile.nofac does not try to compile it.

Otherwise, the installation will likely consist of simply typing "make install" to your shell and waiting for the package to compile and test itself; exceptions are noted below. The package was well tested under SunOS 4.x and 5.x, (RedHat) Linux, HPUX, DEC Ultrix, DEC Alpha OSF, SGI Irix 5.x, and probably others I'm forgetting at the moment; only SunOS 5.x and SGIs using cc presently need Makefile edits, though HPUX and DEC machines benefit from different optimization options. SGIs running Irix 6.x are reported to have problems; please let me know if you do not have problems there or if you can solve them.

If your computer has a 'long double' type with one compiler (say, the FSF's gcc) but not with a different compiler (say, the cc that came with the operating system), using the compiler that has 'long double' should result in substantially faster Lucas-Lehmer test programs, since the FFT run length can often be reduced due to the greater floating point precision.

Extract is reported to compile fine using djgpp on Win/NT 4.0.

Compilation has been at least attempted under OpenVMS on DEC Alphas.

Support for the Amiga has improved, but a gcc compiler problem there is currently stalling further progress, though I have heard nothing on this since the v2.7.2.1 release of gcc.

Other "make" commands

% make clean will remove all of the programs and *.o (object) files. % make all will compile but not test the programs.

Usage information

Nearly all the LL (Lucas-Lehmer) testers and factorers use the same input and command line parsing routine, input() in rw.c, and the LL testers all use the same checkpoint() and printbits() (output) routines, also from rw.c. The factorers' output depends on the factoring package and is therefore in fac*.c where the "*" matches "gmp", "lip", or "int" appropriately. input() also calls a function from fac*.c for reading how high the exponent has already been trial factored.

The usual format read and written by both rw.c and most of the others is documented in mersfmt.txt.

By default, most programs except extract read from stdin and write to stdout. Extract defaults to reading George Woltman's DATABASE file and printing a human-readable copy of it on stdout. Most programs treat arguments that do not start with a "-" as filenames or, if positive integers that do not name files, exponents. "-" by itself means read stdin now rather than not at all or - if no other file arguments are given - only after all other arguments are processed. The usage information for programs that use rw.c's input() is briefly described near the top of rw.c itself and will be printed as part of most error messages related to incorrect usage. Somewhat longer descriptions of options for all of the programs:

-a	For trial factoring only, do not stop on finding the first
        factor; keep looking for more.  Mmtrial and merskfac always
        behave this way.  Otherwise, it is not the default for
        programs using rw.c's input().

-b	For the Lucas-Lehmer test programs, use a (much	faster) binary
	checkpoint format that is not portable between machines nor
	useful via TCP/IP (default).

-c #	Create a new checkpoint after every # iterations
	(Lucas-Lehmer testers only).  Any value less than
	5000 will be treated as 5000.  Overrides the
	FFTLUCAS_CHECKPOINT environment variable, which can also be
	used to set this checkpoint interval.  Default is no
	checkpointing at all.

-d	Duplicate the normal output to stderr.  Mostly so that "-d -n"
	will print the results locally as well as sending them to the
	server.  Can also be given as "-d-", "-d -", "-dstderr", "-d
        stderr", or, to write to stdout, "-dstdout" or "-d stdout",
	or, to write to a particular file, "-dfilename" or "-d
	filename".  Note that "-d -n", e.g. will be treated the same
	as "-dstderr -n".  Default is to not duplicate the output.
	This will never truncate a file; it always appends.

-D      For sprpgmp only, print any pseudo-prime (but not proven prime)
        cofactors.

-e #	For programs that use rw.c's input(), gives the power of two
	at which trial factoring should stop.  As a special case,
	'-e 1' will factor from the prior extent past the next higher
	power of two.  The default is to do the number of trials given
	by -m.

-h	For programs that call rw.c's input(), use the human readable
	checkpoint format, which is portable between computers with
	the same length of double (or long double) and can be sent
	over TCP/IP.  This option is only valid with fftlucas; using
	it with any of the other LL testers will print an error and
	exit to avoid a problem with human readable checkpoint round
	off errors.

-h	For contract, print a brief help message and exit.

-i	For contract, specify the file to read from.

-m #	For trial factoring only, check # (more) possible trial
	factors (values of k in 2*k*p + 1).  Default is one sieve pass
	(which itself defaults to about 98,000 trials, quite small).
	This can also be specified in the input with a line like
	"trials 98000", which all programs using rw.c's input() know
	about and, if not a trial factorer, skip and ignore.  In the
	-n (TCP/IP) case, the server uses "trials #" lines to control
	how long the client runs without interaction.

-o	For programs that use rw.c's input(), -o is like -d, but for
	the "primary" output rather than the duplicate output, but
	this defaults to stdout, including when used like "-o-" and
	"-o -".  This always appends, never truncates.

-o	For contract, specify the output file, but "-o-" will create
        a file named "-" and "-ostdout" will create a file named
        "stdout" (e.g.).

-r # #	For extract, and programs that use rw.c's input(), limit all
        testing to exponents between or equal to the two numbers
        given.  Data for exponents outside the range will be read
        correctly and ignored.  This also applies to input from the
        server via -n; the range is reported to the server on
        connecting to it.

-s	For trial factoring, do a "small" number of trial factors
	for each exponent (this is mostly for the tests in the
	Makefile).  The number of trials is presently one sieve
	pass, presently 98,304 trials if the sieve array's size
	is not changed from the distribution value of 4096.

-t	Print (real or "wall clock") time information to stderr. If
	you have getrusage() and #define USE_RUSAGE, the user and
	system CPU times will also be printed; see below in the
	Makefile section for how to do this.

-u	For contract, ignore information for Mersenne numbers
	of known primality (that is, for composite exponents or for
	which a factor or LL test residue is known).

-v	Print some version information.  Other arguments are processed
	normally; this does not cause an exit.  The version info is
	always sent to the server when connecting to it if the -n flag
	is given.

-v	For contract, increment the verbosity of the output to the
	user (counts of exponents, e.g.).

-1, -2, -3, -4, -m
	For extract only, indicates how many columns to use when
	printing the data from the DATABASE file.  The first three,
	-1, -2, and -3, will print the exponent, the power of two
	indicating the extent of factoring (if -2 or -3), and the flag
	indicating how many Lucas-Lehmer tests have already been
	performed (-3), separated by commas.  -4 and -m print the same
	info as -3, but in the "mers format" that I developed on my own
	before GIMPS was formed: "M( exponent )[UH]: extent".  Nearly
	all of the programs will now read the -1, -2, -4, and -m
	output formats of extract.

Unknown arguments starting with "-" cause error and usage messages to be printed and the program to exit, except in the cases of extract, which simply prints a warning and continues with the next argument, and contract, which ignores them completely. Contract prints a usage message and exits if any argument does not start with a "-".

If FFTLUCAS_CHECKPOINT is in the environment, it is taken to be the number of iterations between writing checkpoint files by the LL test programs (fftlucas, mersenne1, mersenne2, and MacLucasUNIX); it is checked once before all arguments, so a -c command line option will override any value in the environment.

The checkpoint file names are hard-wired to "#", "c#", and "t#" (without the quotes) where # is the exponent being LL-tested. The LL test programs only write to "c#", renaming the prior "c#" to "t#" each time.

The LL test programs (MacLucas*, mersenne1 & 2, and fftlucas) can, of course, read checkpoint files produced by any of the four programs, even binary ones from stdin (as long as any binary checkpoint was produced on a computer of the same architecture).

None of the programs can test an exponent below 31, but all Mersenne numbers with an exponent less than 787 are completely factored anyway, so this limit of 31 may increase in the future. Most of the programs can check any odd exponent Mersenne, but only prime exponent Mersennes can themselves be prime and composite exponent Mersennes have factors that the trial factorers will not check for.

Mersfacint does not run at all if it cannot find a native 64 bit integer type (usually 'long' (DEC Alphas, e.g.) or 'long long' (Intel Linux)).

Other than those that start with "-", extract treats all arguments as file names to open. If the last component (everything after the last "/") of the filename is "DATABASE" or "database", extract assumes it is a copy of George Woltman's old, no longer created by him, DATABASE file and prints it out in a human-readable power-of-2 format documented in mersfmt.txt that can be read by most of the programs. Other arguments are checked to see if they look like checkpoint filenames; if they do, the files are opened and the exponent, FFT run length, and iteration count are read and printed (plus the rest of the first line if it's a human-readable file) to stdout. If they don't look like checkpoint filenames, a warning is printed and extract starts over with the next argument. All lines, except for a DATABASE file, are prefixed by the appropriate filename.

Further installation notes

As noted above, add -DTCPIP to use the TCP/IP networking support; see the top of the Makefile or try:

% make DEFINE=-DTCPIP
If you have getrusage(), adding -DUSE_RUSAGE will print user and system CPU times along with the wall clock time given -t.

For SunOS 5.x (commonly mis-called Solaris, which technically applies to SunOS 4.x as well), see the top of the Makefile for some small editing you need to do before compiling. Or compile using:

% make DEFINE=-DSOLARIS
or:
% make DEFINE="-DSOLARIS -DTCPIP"
if you also want TCPIP support.

For SGI's, setup() calls the SGI-specific function "schedctl(NDPRI, 0, NDPLOMIN)" to reduce the priority as low as possible, completely out of competition with normal programs. If similar things are present in other operating systems, please let me know.

On any computer, look for "OPT" to check the optimization, debugging, and profiling options to gcc or cc. In particular, I'm told that a couple of additional optimizer options will improve the speed by 20-30% under HPUX, different ones will give a 40% or so improvement for some AIX machines, and gcc on SGI's wants to know the exact CPU type; see the Makefile for details.

Also, "make TIME=time" will use the UNIX time program to run the tests to give you an idea of how long they take.

Please, as always, send any comments, questions, bug reports, bug fixes, code suggestions, etc. to me. Please include version numbers of the relevant files, at least of the file with main() in it or of this README.html file. See above for details on a simple way to email me the output from make.


Will Edgington

wedgingt@acm.org (permanent)
wedgingt@gmail.com

Version $Id: README.html,v 8.1 2007/06/23 22:33:35 wedgingt Exp $