2001-02-04 16:28:41 +00:00
|
|
|
/* factor -- print prime factors of n.
|
2010-01-01 10:56:28 +01:00
|
|
|
Copyright (C) 1986, 1995-2005, 2007-2010 Free Software Foundation, Inc.
|
1994-12-12 17:49:55 +00:00
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
1994-12-12 17:49:55 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
1994-12-12 17:49:55 +00:00
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-07-23 14:35:58 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
1994-12-12 17:49:55 +00:00
|
|
|
|
1996-02-29 05:07:34 +00:00
|
|
|
/* Written by Paul Rubin <phr@ocf.berkeley.edu>.
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering.
|
2008-08-04 00:16:06 +01:00
|
|
|
Arbitrary-precision code adapted by James Youngman from Torbjörn
|
|
|
|
|
Granlund's factorize.c, from GNU MP version 4.2.2.
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
*/
|
1994-12-12 17:49:55 +00:00
|
|
|
|
1994-12-20 05:24:13 +00:00
|
|
|
#include <config.h>
|
2004-09-21 22:01:50 +00:00
|
|
|
#include <getopt.h>
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#include <stdarg.h>
|
1994-12-12 17:49:55 +00:00
|
|
|
#include <stdio.h>
|
1994-12-20 05:24:13 +00:00
|
|
|
#include <sys/types.h>
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#if HAVE_GMP
|
2009-09-13 22:05:37 +02:00
|
|
|
# include <gmp.h>
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
1996-02-29 05:07:34 +00:00
|
|
|
#include <assert.h>
|
1994-12-20 05:24:13 +00:00
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
#include "error.h"
|
2005-06-16 21:44:25 +00:00
|
|
|
#include "quote.h"
|
1994-12-31 15:28:51 +00:00
|
|
|
#include "readtokens.h"
|
1999-04-21 03:18:23 +00:00
|
|
|
#include "xstrtol.h"
|
1994-12-31 15:28:51 +00:00
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "factor"
|
|
|
|
|
|
2008-05-19 16:24:27 +02:00
|
|
|
#define AUTHORS proper_name ("Paul Rubin")
|
1999-03-31 04:11:35 +00:00
|
|
|
|
1994-12-31 17:33:56 +00:00
|
|
|
/* Token delimiters when reading from a file. */
|
1994-12-31 15:28:51 +00:00
|
|
|
#define DELIM "\n\t "
|
1994-12-20 05:24:13 +00:00
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
static bool verbose = false;
|
|
|
|
|
|
|
|
|
|
#if HAVE_GMP
|
|
|
|
|
static mpz_t *factor = NULL;
|
|
|
|
|
static size_t nfactors_found = 0;
|
|
|
|
|
static size_t nfactors_allocated = 0;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
debug (char const *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
vfprintf (stderr, fmt, ap);
|
2009-12-12 08:10:07 +01:00
|
|
|
va_end (ap);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
emit_factor (mpz_t n)
|
|
|
|
|
{
|
|
|
|
|
if (nfactors_found == nfactors_allocated)
|
2009-10-08 08:35:55 -06:00
|
|
|
factor = X2NREALLOC (factor, &nfactors_allocated);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
mpz_init (factor[nfactors_found]);
|
|
|
|
|
mpz_set (factor[nfactors_found], n);
|
|
|
|
|
++nfactors_found;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
emit_ul_factor (unsigned long int i)
|
|
|
|
|
{
|
|
|
|
|
mpz_t t;
|
|
|
|
|
mpz_init (t);
|
|
|
|
|
mpz_set_ui (t, i);
|
|
|
|
|
emit_factor (t);
|
2008-12-01 22:39:58 +01:00
|
|
|
mpz_clear (t);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
factor_using_division (mpz_t t, unsigned int limit)
|
|
|
|
|
{
|
|
|
|
|
mpz_t q, r;
|
|
|
|
|
unsigned long int f;
|
|
|
|
|
int ai;
|
|
|
|
|
static unsigned int const add[] = {4, 2, 4, 2, 4, 6, 2, 6};
|
|
|
|
|
unsigned int const *addv = add;
|
|
|
|
|
unsigned int failures;
|
|
|
|
|
|
|
|
|
|
debug ("[trial division (%u)] ", limit);
|
|
|
|
|
|
|
|
|
|
mpz_init (q);
|
|
|
|
|
mpz_init (r);
|
|
|
|
|
|
|
|
|
|
f = mpz_scan1 (t, 0);
|
|
|
|
|
mpz_div_2exp (t, t, f);
|
|
|
|
|
while (f)
|
|
|
|
|
{
|
|
|
|
|
emit_ul_factor (2);
|
|
|
|
|
--f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
mpz_tdiv_qr_ui (q, r, t, 3);
|
|
|
|
|
if (mpz_cmp_ui (r, 0) != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
break;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
mpz_set (t, q);
|
|
|
|
|
emit_ul_factor (3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
mpz_tdiv_qr_ui (q, r, t, 5);
|
|
|
|
|
if (mpz_cmp_ui (r, 0) != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
break;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
mpz_set (t, q);
|
|
|
|
|
emit_ul_factor (5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failures = 0;
|
|
|
|
|
f = 7;
|
|
|
|
|
ai = 0;
|
|
|
|
|
while (mpz_cmp_ui (t, 1) != 0)
|
|
|
|
|
{
|
|
|
|
|
mpz_tdiv_qr_ui (q, r, t, f);
|
|
|
|
|
if (mpz_cmp_ui (r, 0) != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
f += addv[ai];
|
|
|
|
|
if (mpz_cmp_ui (q, f) < 0)
|
|
|
|
|
break;
|
|
|
|
|
ai = (ai + 1) & 7;
|
|
|
|
|
failures++;
|
|
|
|
|
if (failures > limit)
|
|
|
|
|
break;
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
mpz_swap (t, q);
|
|
|
|
|
emit_ul_factor (f);
|
|
|
|
|
failures = 0;
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mpz_clear (q);
|
|
|
|
|
mpz_clear (r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
factor_using_pollard_rho (mpz_t n, int a_int)
|
|
|
|
|
{
|
|
|
|
|
mpz_t x, x1, y, P;
|
|
|
|
|
mpz_t a;
|
|
|
|
|
mpz_t g;
|
|
|
|
|
mpz_t t1, t2;
|
|
|
|
|
int k, l, c, i;
|
|
|
|
|
|
|
|
|
|
debug ("[pollard-rho (%d)] ", a_int);
|
|
|
|
|
|
|
|
|
|
mpz_init (g);
|
|
|
|
|
mpz_init (t1);
|
|
|
|
|
mpz_init (t2);
|
|
|
|
|
|
|
|
|
|
mpz_init_set_si (a, a_int);
|
|
|
|
|
mpz_init_set_si (y, 2);
|
|
|
|
|
mpz_init_set_si (x, 2);
|
|
|
|
|
mpz_init_set_si (x1, 2);
|
|
|
|
|
k = 1;
|
|
|
|
|
l = 1;
|
|
|
|
|
mpz_init_set_ui (P, 1);
|
|
|
|
|
c = 0;
|
|
|
|
|
|
|
|
|
|
while (mpz_cmp_ui (n, 1) != 0)
|
|
|
|
|
{
|
|
|
|
|
S2:
|
|
|
|
|
mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n);
|
|
|
|
|
|
|
|
|
|
mpz_sub (t1, x1, x); mpz_mul (t2, P, t1); mpz_mod (P, t2, n);
|
|
|
|
|
c++;
|
|
|
|
|
if (c == 20)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
c = 0;
|
|
|
|
|
mpz_gcd (g, P, n);
|
|
|
|
|
if (mpz_cmp_ui (g, 1) != 0)
|
|
|
|
|
goto S4;
|
|
|
|
|
mpz_set (y, x);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
|
|
|
|
k--;
|
|
|
|
|
if (k > 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
goto S2;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
|
|
|
|
mpz_gcd (g, P, n);
|
|
|
|
|
if (mpz_cmp_ui (g, 1) != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
goto S4;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
|
|
|
|
mpz_set (x1, x);
|
|
|
|
|
k = l;
|
|
|
|
|
l = 2 * l;
|
|
|
|
|
for (i = 0; i < k; i++)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
mpz_mul (x, x, x); mpz_add (x, x, a); mpz_mod (x, x, n);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
mpz_set (y, x);
|
|
|
|
|
c = 0;
|
|
|
|
|
goto S2;
|
|
|
|
|
S4:
|
|
|
|
|
do
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
mpz_mul (y, y, y); mpz_add (y, y, a); mpz_mod (y, y, n);
|
|
|
|
|
mpz_sub (t1, x1, y); mpz_gcd (g, t1, n);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
while (mpz_cmp_ui (g, 1) == 0);
|
|
|
|
|
|
|
|
|
|
mpz_div (n, n, g); /* divide by g, before g is overwritten */
|
|
|
|
|
|
|
|
|
|
if (!mpz_probab_prime_p (g, 3))
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
mp_limb_t a_limb;
|
|
|
|
|
mpn_random (&a_limb, (mp_size_t) 1);
|
|
|
|
|
a_int = (int) a_limb;
|
|
|
|
|
}
|
|
|
|
|
while (a_int == -2 || a_int == 0);
|
|
|
|
|
|
|
|
|
|
debug ("[composite factor--restarting pollard-rho] ");
|
|
|
|
|
factor_using_pollard_rho (g, a_int);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
else
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
emit_factor (g);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
mpz_mod (x, x, n);
|
|
|
|
|
mpz_mod (x1, x1, n);
|
|
|
|
|
mpz_mod (y, y, n);
|
|
|
|
|
if (mpz_probab_prime_p (n, 3))
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
emit_factor (n);
|
|
|
|
|
break;
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mpz_clear (g);
|
|
|
|
|
mpz_clear (P);
|
|
|
|
|
mpz_clear (t2);
|
|
|
|
|
mpz_clear (t1);
|
|
|
|
|
mpz_clear (a);
|
|
|
|
|
mpz_clear (x1);
|
|
|
|
|
mpz_clear (x);
|
|
|
|
|
mpz_clear (y);
|
|
|
|
|
}
|
2008-10-25 22:51:54 +02:00
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
debug (char const *fmt ATTRIBUTE_UNUSED, ...)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
2005-10-03 12:12:14 +00:00
|
|
|
/* The maximum number of factors, including -1, for negative numbers. */
|
|
|
|
|
#define MAX_N_FACTORS (sizeof (uintmax_t) * CHAR_BIT)
|
1994-12-31 17:33:56 +00:00
|
|
|
|
2001-02-03 13:37:37 +00:00
|
|
|
/* The trial divisor increment wheel. Use it to skip over divisors that
|
|
|
|
|
are composites of 2, 3, 5, 7, or 11. The part from WHEEL_START up to
|
|
|
|
|
WHEEL_END is reused periodically, while the "lead in" is used to test
|
|
|
|
|
for those primes and to jump onto the wheel. For more information, see
|
|
|
|
|
http://www.utm.edu/research/primes/glossary/WheelFactorization.html */
|
|
|
|
|
|
2001-04-24 07:16:28 +00:00
|
|
|
#include "wheel-size.h" /* For the definition of WHEEL_SIZE. */
|
2004-08-03 06:15:49 +00:00
|
|
|
static const unsigned char wheel_tab[] =
|
2001-04-24 07:16:28 +00:00
|
|
|
{
|
|
|
|
|
#include "wheel.h"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define WHEEL_START (wheel_tab + WHEEL_SIZE)
|
2009-05-17 13:02:25 +02:00
|
|
|
#define WHEEL_END (wheel_tab + ARRAY_CARDINALITY (wheel_tab))
|
2001-02-03 13:37:37 +00:00
|
|
|
|
1996-02-29 05:07:34 +00:00
|
|
|
/* FIXME: comment */
|
|
|
|
|
|
2004-08-03 06:15:49 +00:00
|
|
|
static size_t
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
factor_wheel (uintmax_t n0, size_t max_n_factors, uintmax_t *factors)
|
1994-12-12 17:49:55 +00:00
|
|
|
{
|
2005-03-06 16:25:26 +00:00
|
|
|
uintmax_t n = n0, d, q;
|
2004-08-03 06:15:49 +00:00
|
|
|
size_t n_factors = 0;
|
|
|
|
|
unsigned char const *w = wheel_tab;
|
1994-12-12 17:49:55 +00:00
|
|
|
|
2004-12-07 21:11:59 +00:00
|
|
|
if (n <= 1)
|
1994-12-20 05:24:13 +00:00
|
|
|
return n_factors;
|
1994-12-12 17:49:55 +00:00
|
|
|
|
|
|
|
|
/* The exit condition in the following loop is correct because
|
|
|
|
|
any time it is tested one of these 3 conditions holds:
|
|
|
|
|
(1) d divides n
|
|
|
|
|
(2) n is prime
|
|
|
|
|
(3) n is composite but has no factors less than d.
|
|
|
|
|
If (1) or (2) obviously the right thing happens.
|
|
|
|
|
If (3), then since n is composite it is >= d^2. */
|
1997-02-20 05:11:12 +00:00
|
|
|
|
2001-02-03 13:37:37 +00:00
|
|
|
d = 2;
|
1997-02-20 05:11:12 +00:00
|
|
|
do
|
1994-12-12 17:49:55 +00:00
|
|
|
{
|
1997-02-20 05:11:12 +00:00
|
|
|
q = n / d;
|
|
|
|
|
while (n == q * d)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
assert (n_factors < max_n_factors);
|
|
|
|
|
factors[n_factors++] = d;
|
|
|
|
|
n = q;
|
|
|
|
|
q = n / d;
|
|
|
|
|
}
|
2001-02-03 13:37:37 +00:00
|
|
|
d += *(w++);
|
|
|
|
|
if (w == WHEEL_END)
|
2009-08-22 18:56:06 +02:00
|
|
|
w = WHEEL_START;
|
1994-12-12 17:49:55 +00:00
|
|
|
}
|
1997-02-20 05:11:12 +00:00
|
|
|
while (d <= q);
|
|
|
|
|
|
1994-12-12 17:49:55 +00:00
|
|
|
if (n != 1 || n0 == 1)
|
1994-12-31 17:33:56 +00:00
|
|
|
{
|
|
|
|
|
assert (n_factors < max_n_factors);
|
|
|
|
|
factors[n_factors++] = n;
|
|
|
|
|
}
|
1994-12-20 05:24:13 +00:00
|
|
|
|
|
|
|
|
return n_factors;
|
|
|
|
|
}
|
|
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
/* Single-precision factoring */
|
2008-10-24 22:43:40 +02:00
|
|
|
static void
|
|
|
|
|
print_factors_single (uintmax_t n)
|
1994-12-20 05:24:13 +00:00
|
|
|
{
|
1999-04-21 03:18:23 +00:00
|
|
|
uintmax_t factors[MAX_N_FACTORS];
|
2008-10-24 22:43:40 +02:00
|
|
|
size_t n_factors = factor_wheel (n, MAX_N_FACTORS, factors);
|
2004-08-03 06:15:49 +00:00
|
|
|
size_t i;
|
2002-11-05 20:17:28 +00:00
|
|
|
char buf[INT_BUFSIZE_BOUND (uintmax_t)];
|
1994-12-20 05:24:13 +00:00
|
|
|
|
2002-11-05 20:17:28 +00:00
|
|
|
printf ("%s:", umaxtostr (n, buf));
|
1994-12-31 17:33:56 +00:00
|
|
|
for (i = 0; i < n_factors; i++)
|
2002-11-05 20:17:28 +00:00
|
|
|
printf (" %s", umaxtostr (factors[i], buf));
|
1998-06-29 16:24:01 +00:00
|
|
|
putchar ('\n');
|
1994-12-12 17:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#if HAVE_GMP
|
|
|
|
|
static int
|
|
|
|
|
mpcompare (const void *av, const void *bv)
|
|
|
|
|
{
|
|
|
|
|
mpz_t *const *a = av;
|
|
|
|
|
mpz_t *const *b = bv;
|
|
|
|
|
return mpz_cmp (**a, **b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sort_and_print_factors (void)
|
|
|
|
|
{
|
|
|
|
|
mpz_t **faclist;
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
faclist = xcalloc (nfactors_found, sizeof *faclist);
|
|
|
|
|
for (i = 0; i < nfactors_found; ++i)
|
|
|
|
|
{
|
|
|
|
|
faclist[i] = &factor[i];
|
|
|
|
|
}
|
|
|
|
|
qsort (faclist, nfactors_found, sizeof *faclist, mpcompare);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nfactors_found; ++i)
|
|
|
|
|
{
|
|
|
|
|
fputc (' ', stdout);
|
|
|
|
|
mpz_out_str (stdout, 10, *faclist[i]);
|
|
|
|
|
}
|
|
|
|
|
putchar ('\n');
|
|
|
|
|
free (faclist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_factors (void)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nfactors_found; ++i)
|
|
|
|
|
{
|
|
|
|
|
mpz_clear (factor[i]);
|
|
|
|
|
}
|
|
|
|
|
/* Don't actually free factor[] because in the case where we are
|
|
|
|
|
reading numbers from stdin, we may be about to use it again. */
|
|
|
|
|
nfactors_found = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-24 22:43:40 +02:00
|
|
|
/* Arbitrary-precision factoring */
|
|
|
|
|
static void
|
|
|
|
|
print_factors_multi (mpz_t t)
|
|
|
|
|
{
|
|
|
|
|
mpz_out_str (stdout, 10, t);
|
|
|
|
|
putchar (':');
|
|
|
|
|
|
|
|
|
|
if (mpz_sgn (t) != 0)
|
|
|
|
|
{
|
|
|
|
|
/* Set the trial division limit according to the size of t. */
|
|
|
|
|
size_t n_bits = mpz_sizeinbase (t, 2);
|
|
|
|
|
unsigned int division_limit = MIN (n_bits, 1000);
|
|
|
|
|
division_limit *= division_limit;
|
|
|
|
|
|
|
|
|
|
factor_using_division (t, division_limit);
|
|
|
|
|
|
|
|
|
|
if (mpz_cmp_ui (t, 1) != 0)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
debug ("[is number prime?] ");
|
|
|
|
|
if (mpz_probab_prime_p (t, 3))
|
|
|
|
|
emit_factor (t);
|
|
|
|
|
else
|
|
|
|
|
factor_using_pollard_rho (t, 1);
|
|
|
|
|
}
|
2008-10-24 22:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mpz_clear (t);
|
|
|
|
|
sort_and_print_factors ();
|
|
|
|
|
free_factors ();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
|
|
|
|
/* Emit the factors of the indicated number. If we have the option of using
|
|
|
|
|
either algorithm, we select on the basis of the length of the number.
|
|
|
|
|
For longer numbers, we prefer the MP algorithm even if the native algorithm
|
2008-10-24 22:43:40 +02:00
|
|
|
has enough digits, because the algorithm is better. The turnover point
|
|
|
|
|
depends on the value. */
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
static bool
|
|
|
|
|
print_factors (char const *s)
|
|
|
|
|
{
|
2008-10-24 22:43:40 +02:00
|
|
|
uintmax_t n;
|
|
|
|
|
strtol_error err = xstrtoumax (s, NULL, 10, &n, "");
|
|
|
|
|
|
|
|
|
|
#if HAVE_GMP
|
|
|
|
|
enum { GMP_TURNOVER_POINT = 100000 };
|
|
|
|
|
|
|
|
|
|
if (err == LONGINT_OVERFLOW
|
|
|
|
|
|| (err == LONGINT_OK && GMP_TURNOVER_POINT <= n))
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
{
|
2008-10-24 22:43:40 +02:00
|
|
|
mpz_t t;
|
|
|
|
|
mpz_init (t);
|
|
|
|
|
if (gmp_sscanf (s, "%Zd", t) == 1)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
debug ("[%s]", _("using arbitrary-precision arithmetic"));
|
|
|
|
|
print_factors_multi (t);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-10-24 22:43:40 +02:00
|
|
|
err = LONGINT_INVALID;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
2008-10-24 22:43:40 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
switch (err)
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
{
|
2008-10-24 22:43:40 +02:00
|
|
|
case LONGINT_OK:
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
debug ("[%s]", _("using single-precision arithmetic"));
|
2008-10-24 22:43:40 +02:00
|
|
|
print_factors_single (n);
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case LONGINT_OVERFLOW:
|
|
|
|
|
error (0, 0, _("%s is too large"), quote (s));
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
error (0, 0, _("%s is not a valid positive integer"), quote (s));
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
2008-10-24 22:43:40 +02:00
|
|
|
VERBOSE_OPTION = CHAR_MAX + 1
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct option const long_options[] =
|
|
|
|
|
{
|
|
|
|
|
{"verbose", no_argument, NULL, VERBOSE_OPTION},
|
|
|
|
|
{GETOPT_HELP_OPTION_DECL},
|
|
|
|
|
{GETOPT_VERSION_OPTION_DECL},
|
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
usage (int status)
|
|
|
|
|
{
|
|
|
|
|
if (status != EXIT_SUCCESS)
|
|
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
2009-08-22 18:56:06 +02:00
|
|
|
program_name);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf (_("\
|
|
|
|
|
Usage: %s [NUMBER]...\n\
|
|
|
|
|
or: %s OPTION\n\
|
|
|
|
|
"),
|
2009-08-22 18:56:06 +02:00
|
|
|
program_name, program_name);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
fputs (_("\
|
2008-08-09 14:52:54 +02:00
|
|
|
Print the prime factors of each specified integer NUMBER. If none\n\
|
|
|
|
|
are specified on the command line, read them from standard input.\n\
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
\n\
|
|
|
|
|
"), stdout);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2009-09-18 23:06:21 +01:00
|
|
|
emit_ancillary_info ();
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
|
|
|
|
exit (status);
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-03 06:15:49 +00:00
|
|
|
static bool
|
1996-01-06 11:44:05 +00:00
|
|
|
do_stdin (void)
|
1994-12-12 17:49:55 +00:00
|
|
|
{
|
2004-08-03 06:15:49 +00:00
|
|
|
bool ok = true;
|
1994-12-31 15:28:51 +00:00
|
|
|
token_buffer tokenbuffer;
|
|
|
|
|
|
|
|
|
|
init_tokenbuffer (&tokenbuffer);
|
1994-12-12 17:49:55 +00:00
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2004-03-21 18:45:37 +00:00
|
|
|
size_t token_length = readtoken (stdin, DELIM, sizeof (DELIM) - 1,
|
2009-08-22 18:56:06 +02:00
|
|
|
&tokenbuffer);
|
2004-03-21 18:45:37 +00:00
|
|
|
if (token_length == (size_t) -1)
|
2009-08-22 18:56:06 +02:00
|
|
|
break;
|
2004-08-03 06:15:49 +00:00
|
|
|
ok &= print_factors (tokenbuffer.buffer);
|
1994-12-12 17:49:55 +00:00
|
|
|
}
|
1994-12-31 15:28:51 +00:00
|
|
|
free (tokenbuffer.buffer);
|
1996-02-29 05:07:34 +00:00
|
|
|
|
2004-08-03 06:15:49 +00:00
|
|
|
return ok;
|
1994-12-12 17:49:55 +00:00
|
|
|
}
|
1994-12-13 05:42:44 +00:00
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1994-12-13 05:42:44 +00:00
|
|
|
{
|
2004-08-03 06:15:49 +00:00
|
|
|
bool ok;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
int c;
|
1996-02-29 05:07:34 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
2008-06-03 08:34:09 +02:00
|
|
|
set_program_name (argv[0]);
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
1994-12-20 05:24:13 +00:00
|
|
|
|
2000-05-07 14:52:54 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
|
|
|
|
|
{
|
|
|
|
|
switch (c)
|
2009-08-22 18:56:06 +02:00
|
|
|
{
|
|
|
|
|
case VERBOSE_OPTION:
|
|
|
|
|
verbose = true;
|
|
|
|
|
break;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_HELP_CHAR;
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
|
2009-08-22 18:56:06 +02:00
|
|
|
default:
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
}
|
1994-12-20 05:24:13 +00:00
|
|
|
|
2004-09-21 22:01:50 +00:00
|
|
|
if (argc <= optind)
|
2004-08-03 06:15:49 +00:00
|
|
|
ok = do_stdin ();
|
1994-12-13 05:42:44 +00:00
|
|
|
else
|
|
|
|
|
{
|
1996-02-29 05:07:34 +00:00
|
|
|
int i;
|
2005-10-01 08:01:25 +00:00
|
|
|
ok = true;
|
2004-09-21 22:01:50 +00:00
|
|
|
for (i = optind; i < argc; i++)
|
2009-08-22 18:56:06 +02:00
|
|
|
if (! print_factors (argv[i]))
|
|
|
|
|
ok = false;
|
1994-12-13 05:42:44 +00:00
|
|
|
}
|
factor arbitrarily large numbers
* m4/gmp.m4: New file; adds cu_GMP, which detects GNU MP.
* configure.ac: Use cu_GMP.
* src/Makefile.am: Link factor against libgmp if available.
* src/factor.c: Use GNU MP if it is available.
(emit_factor, emit_ul_factor, factor_using_division,
factor_using_pollard_rho, extract_factors_multi,
sort_and_print_factors, free_factors): new functions
for the arbitrary-precision implementation, taken from an example
in GNU MP.
(factor_wheel): Renamed; was called factor.
(print_factors_single): Renamed; was called print_factors.
(print_factors): New function, chooses between the single- and
arbitrary-precision algorithms according to availability of GNU MP
and the length of the number to be factored.
(usage, main): New options --bignum and --no-bignum.
* coreutils.texi (factor invocation): Document new command-line
options for the MP implementation and update the performance
numbers to take into account the asymptotically faster algorithm.
* TODO: Remove item about factoring large primes (it's done).
* m4/gmp.m4: Add support for --without-gmp.
* NEWS: Mention the new feature.
2008-07-31 09:58:10 +02:00
|
|
|
#if HAVE_GMP
|
|
|
|
|
free (factor);
|
|
|
|
|
#endif
|
2004-08-03 06:15:49 +00:00
|
|
|
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
1994-12-13 05:42:44 +00:00
|
|
|
}
|