1 /* factor -- print prime factors of n.
2 Copyright (C) 1986, 1995-2005, 2007-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin <phr@ocf.berkeley.edu>.
18 Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering.
19 Arbitrary-precision code adapted by James Youngman from Torbjörn
20 Granlund's factorize.c, from GNU MP version 4.2.2.
27 #include <sys/types.h>
37 #include "readtokens.h"
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "factor"
43 #define AUTHORS proper_name ("Paul Rubin")
45 /* Token delimiters when reading from a file. */
48 static bool verbose
= false;
51 static mpz_t
*factor
= NULL
;
52 static size_t nfactors_found
= 0;
53 static size_t nfactors_allocated
= 0;
56 debug (char const *fmt
, ...)
62 vfprintf (stderr
, fmt
, ap
);
70 if (nfactors_found
== nfactors_allocated
)
71 factor
= X2NREALLOC (factor
, &nfactors_allocated
);
72 mpz_init (factor
[nfactors_found
]);
73 mpz_set (factor
[nfactors_found
], n
);
78 emit_ul_factor (unsigned long int i
)
88 factor_using_division (mpz_t t
, unsigned int limit
)
93 static unsigned int const add
[] = {4, 2, 4, 2, 4, 6, 2, 6};
94 unsigned int const *addv
= add
;
95 unsigned int failures
;
97 debug ("[trial division (%u)] ", limit
);
102 f
= mpz_scan1 (t
, 0);
103 mpz_div_2exp (t
, t
, f
);
112 mpz_tdiv_qr_ui (q
, r
, t
, 3);
113 if (mpz_cmp_ui (r
, 0) != 0)
121 mpz_tdiv_qr_ui (q
, r
, t
, 5);
122 if (mpz_cmp_ui (r
, 0) != 0)
131 while (mpz_cmp_ui (t
, 1) != 0)
133 mpz_tdiv_qr_ui (q
, r
, t
, f
);
134 if (mpz_cmp_ui (r
, 0) != 0)
137 if (mpz_cmp_ui (q
, f
) < 0)
141 if (failures
> limit
)
157 factor_using_pollard_rho (mpz_t n
, int a_int
)
165 debug ("[pollard-rho (%d)] ", a_int
);
171 mpz_init_set_si (a
, a_int
);
172 mpz_init_set_si (y
, 2);
173 mpz_init_set_si (x
, 2);
174 mpz_init_set_si (x1
, 2);
177 mpz_init_set_ui (P
, 1);
180 while (mpz_cmp_ui (n
, 1) != 0)
183 mpz_mul (x
, x
, x
); mpz_add (x
, x
, a
); mpz_mod (x
, x
, n
);
185 mpz_sub (t1
, x1
, x
); mpz_mul (t2
, P
, t1
); mpz_mod (P
, t2
, n
);
191 if (mpz_cmp_ui (g
, 1) != 0)
201 if (mpz_cmp_ui (g
, 1) != 0)
207 for (i
= 0; i
< k
; i
++)
209 mpz_mul (x
, x
, x
); mpz_add (x
, x
, a
); mpz_mod (x
, x
, n
);
217 mpz_mul (y
, y
, y
); mpz_add (y
, y
, a
); mpz_mod (y
, y
, n
);
218 mpz_sub (t1
, x1
, y
); mpz_gcd (g
, t1
, n
);
220 while (mpz_cmp_ui (g
, 1) == 0);
222 mpz_div (n
, n
, g
); /* divide by g, before g is overwritten */
224 if (!mpz_probab_prime_p (g
, 3))
229 mpn_random (&a_limb
, (mp_size_t
) 1);
230 a_int
= (int) a_limb
;
232 while (a_int
== -2 || a_int
== 0);
234 debug ("[composite factor--restarting pollard-rho] ");
235 factor_using_pollard_rho (g
, a_int
);
244 if (mpz_probab_prime_p (n
, 3))
264 debug (char const *fmt ATTRIBUTE_UNUSED
, ...)
270 /* The maximum number of factors, including -1, for negative numbers. */
271 #define MAX_N_FACTORS (sizeof (uintmax_t) * CHAR_BIT)
273 /* The trial divisor increment wheel. Use it to skip over divisors that
274 are composites of 2, 3, 5, 7, or 11. The part from WHEEL_START up to
275 WHEEL_END is reused periodically, while the "lead in" is used to test
276 for those primes and to jump onto the wheel. For more information, see
277 http://www.utm.edu/research/primes/glossary/WheelFactorization.html */
279 #include "wheel-size.h" /* For the definition of WHEEL_SIZE. */
280 static const unsigned char wheel_tab
[] =
285 #define WHEEL_START (wheel_tab + WHEEL_SIZE)
286 #define WHEEL_END (wheel_tab + ARRAY_CARDINALITY (wheel_tab))
291 factor_wheel (uintmax_t n0
, size_t max_n_factors
, uintmax_t *factors
)
293 uintmax_t n
= n0
, d
, q
;
294 size_t n_factors
= 0;
295 unsigned char const *w
= wheel_tab
;
300 /* The exit condition in the following loop is correct because
301 any time it is tested one of these 3 conditions holds:
304 (3) n is composite but has no factors less than d.
305 If (1) or (2) obviously the right thing happens.
306 If (3), then since n is composite it is >= d^2. */
314 assert (n_factors
< max_n_factors
);
315 factors
[n_factors
++] = d
;
325 if (n
!= 1 || n0
== 1)
327 assert (n_factors
< max_n_factors
);
328 factors
[n_factors
++] = n
;
334 /* Single-precision factoring */
336 print_factors_single (uintmax_t n
)
338 uintmax_t factors
[MAX_N_FACTORS
];
339 size_t n_factors
= factor_wheel (n
, MAX_N_FACTORS
, factors
);
341 char buf
[INT_BUFSIZE_BOUND (uintmax_t)];
343 printf ("%s:", umaxtostr (n
, buf
));
344 for (i
= 0; i
< n_factors
; i
++)
345 printf (" %s", umaxtostr (factors
[i
], buf
));
351 mpcompare (const void *av
, const void *bv
)
353 mpz_t
*const *a
= av
;
354 mpz_t
*const *b
= bv
;
355 return mpz_cmp (**a
, **b
);
359 sort_and_print_factors (void)
364 faclist
= xcalloc (nfactors_found
, sizeof *faclist
);
365 for (i
= 0; i
< nfactors_found
; ++i
)
367 faclist
[i
] = &factor
[i
];
369 qsort (faclist
, nfactors_found
, sizeof *faclist
, mpcompare
);
371 for (i
= 0; i
< nfactors_found
; ++i
)
374 mpz_out_str (stdout
, 10, *faclist
[i
]);
385 for (i
= 0; i
< nfactors_found
; ++i
)
387 mpz_clear (factor
[i
]);
389 /* Don't actually free factor[] because in the case where we are
390 reading numbers from stdin, we may be about to use it again. */
394 /* Arbitrary-precision factoring */
396 print_factors_multi (mpz_t t
)
398 mpz_out_str (stdout
, 10, t
);
401 if (mpz_sgn (t
) != 0)
403 /* Set the trial division limit according to the size of t. */
404 size_t n_bits
= mpz_sizeinbase (t
, 2);
405 unsigned int division_limit
= MIN (n_bits
, 1000);
406 division_limit
*= division_limit
;
408 factor_using_division (t
, division_limit
);
410 if (mpz_cmp_ui (t
, 1) != 0)
412 debug ("[is number prime?] ");
413 if (mpz_probab_prime_p (t
, 3))
416 factor_using_pollard_rho (t
, 1);
421 sort_and_print_factors ();
427 /* Emit the factors of the indicated number. If we have the option of using
428 either algorithm, we select on the basis of the length of the number.
429 For longer numbers, we prefer the MP algorithm even if the native algorithm
430 has enough digits, because the algorithm is better. The turnover point
431 depends on the value. */
433 print_factors (char const *s
)
436 strtol_error err
= xstrtoumax (s
, NULL
, 10, &n
, "");
439 enum { GMP_TURNOVER_POINT
= 100000 };
441 if (err
== LONGINT_OVERFLOW
442 || (err
== LONGINT_OK
&& GMP_TURNOVER_POINT
<= n
))
446 if (gmp_sscanf (s
, "%Zd", t
) == 1)
448 debug ("[%s]", _("using arbitrary-precision arithmetic"));
449 print_factors_multi (t
);
452 err
= LONGINT_INVALID
;
459 debug ("[%s]", _("using single-precision arithmetic"));
460 print_factors_single (n
);
463 case LONGINT_OVERFLOW
:
464 error (0, 0, _("%s is too large"), quote (s
));
468 error (0, 0, _("%s is not a valid positive integer"), quote (s
));
475 VERBOSE_OPTION
= CHAR_MAX
+ 1
478 static struct option
const long_options
[] =
480 {"verbose", no_argument
, NULL
, VERBOSE_OPTION
},
481 {GETOPT_HELP_OPTION_DECL
},
482 {GETOPT_VERSION_OPTION_DECL
},
489 if (status
!= EXIT_SUCCESS
)
490 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
495 Usage: %s [NUMBER]...\n\
498 program_name
, program_name
);
500 Print the prime factors of each specified integer NUMBER. If none\n\
501 are specified on the command line, read them from standard input.\n\
504 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
505 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
506 emit_ancillary_info ();
515 token_buffer tokenbuffer
;
517 init_tokenbuffer (&tokenbuffer
);
521 size_t token_length
= readtoken (stdin
, DELIM
, sizeof (DELIM
) - 1,
523 if (token_length
== (size_t) -1)
525 ok
&= print_factors (tokenbuffer
.buffer
);
527 free (tokenbuffer
.buffer
);
533 main (int argc
, char **argv
)
538 initialize_main (&argc
, &argv
);
539 set_program_name (argv
[0]);
540 setlocale (LC_ALL
, "");
541 bindtextdomain (PACKAGE
, LOCALEDIR
);
542 textdomain (PACKAGE
);
544 atexit (close_stdout
);
546 while ((c
= getopt_long (argc
, argv
, "", long_options
, NULL
)) != -1)
554 case_GETOPT_HELP_CHAR
;
556 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
559 usage (EXIT_FAILURE
);
569 for (i
= optind
; i
< argc
; i
++)
570 if (! print_factors (argv
[i
]))
576 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);