1 /* factor -- print factors of n. lose if n > 2^32.
2 Copyright (C) 86, 1995 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 2, or (at your option)
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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Paul Rubin <phr@ocf.berkeley.edu>.
19 Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering. */
24 #include <sys/types.h>
31 #endif /* HAVE_LIMITS_H */
34 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
38 # define INT_MAX ((int) (UINT_MAX >> 1))
42 #include "long-options.h"
45 #include "readtokens.h"
47 /* Token delimiters when reading from a file. */
50 /* FIXME: if this program is ever modified to factor integers larger
51 than 2^128, this constant (and the algorithm :-) will have to change. */
52 #define MAX_N_FACTORS 128
54 /* The name this program was run with. */
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
66 Usage: %s [NUMBER...]\n\
69 program_name, program_name);
72 --help display this help and exit\n\
73 --version output version information and exit\n\
82 factor (long unsigned int n0, int max_n_factors, long unsigned int *factors)
84 register unsigned long n = n0, d;
93 assert (n_factors < max_n_factors);
94 factors[n_factors++] = 2;
98 /* The exit condition in the following loop is correct because
99 any time it is tested one of these 3 conditions holds:
102 (3) n is composite but has no factors less than d.
103 If (1) or (2) obviously the right thing happens.
104 If (3), then since n is composite it is >= d^2. */
105 sqrt_n = (unsigned int) sqrt ((double) n);
106 for (d = 3; d <= sqrt_n; d += 2)
110 assert (n_factors < max_n_factors);
111 factors[n_factors++] = d;
115 if (n != 1 || n0 == 1)
117 assert (n_factors < max_n_factors);
118 factors[n_factors++] = n;
127 print_factors (const char *s)
129 unsigned long int factors[MAX_N_FACTORS];
134 if (xstrtoul (s, NULL, 10, &n, NULL) != LONGINT_OK)
136 error (0, 0, _("%s: invalid argument"), s);
139 n_factors = factor (n, MAX_N_FACTORS, factors);
141 for (i = 0; i < n_factors; i++)
142 printf (" %lu", factors[i]);
151 token_buffer tokenbuffer;
153 init_tokenbuffer (&tokenbuffer);
157 long int token_length;
159 token_length = readtoken (stdin, DELIM, sizeof (DELIM) - 1,
161 if (token_length < 0)
163 fail |= print_factors (tokenbuffer.buffer);
165 free (tokenbuffer.buffer);
171 main (int argc, char **argv)
175 program_name = argv[0];
176 setlocale (LC_ALL, "");
177 bindtextdomain (PACKAGE, LOCALEDIR);
178 textdomain (PACKAGE);
180 parse_long_options (argc, argv, "factor", PACKAGE_VERSION, usage);
188 for (i = 1; i < argc; i++)
189 fail |= print_factors (argv[i]);