1 /* factor -- print factors of n.
2 Copyright (C) 86, 95, 96, 1997, 1998, 1999 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin <phr@ocf.berkeley.edu>.
19 Adapted for GNU, fixed to factor UINT_MAX by Jim Meyering. */
23 #include <sys/types.h>
29 #include "long-options.h"
32 #include "readtokens.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "factor"
38 #define AUTHORS "Paul Rubin"
40 /* Token delimiters when reading from a file. */
43 /* FIXME: if this program is ever modified to factor integers larger
44 than 2^128, this constant (and the algorithm :-) will have to change. */
45 #define MAX_N_FACTORS 128
47 /* The name this program was run with. */
54 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
59 Usage: %s [NUMBER]...\n\
62 program_name
, program_name
);
64 Print factors of each NUMBER; read standard input with no arguments.\n\
66 --help display this help and exit\n\
67 --version output version information and exit\n\
69 Print the prime factors of all specified integer NUMBERs. If no arguments\n\
70 are specified on the command line, they are read from standard input.\n\
72 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
80 factor (uintmax_t n0
, int max_n_factors
, uintmax_t *factors
)
82 register uintmax_t n
= n0
, d
, q
;
90 assert (n_factors
< max_n_factors
);
91 factors
[n_factors
++] = 2;
95 /* The exit condition in the following loop is correct because
96 any time it is tested one of these 3 conditions holds:
99 (3) n is composite but has no factors less than d.
100 If (1) or (2) obviously the right thing happens.
101 If (3), then since n is composite it is >= d^2. */
109 assert (n_factors
< max_n_factors
);
110 factors
[n_factors
++] = d
;
118 if (n
!= 1 || n0
== 1)
120 assert (n_factors
< max_n_factors
);
121 factors
[n_factors
++] = n
;
130 print_factors (const char *s
)
132 uintmax_t factors
[MAX_N_FACTORS
];
136 char buf
[LONGEST_HUMAN_READABLE
+ 1];
138 if (xstrtoumax (s
, NULL
, 10, &n
, "") != LONGINT_OK
)
140 error (0, 0, _("`%s' is not a valid positive integer"), s
);
143 n_factors
= factor (n
, MAX_N_FACTORS
, factors
);
144 printf ("%s:", human_readable (n
, buf
, 1, 1));
145 for (i
= 0; i
< n_factors
; i
++)
146 printf (" %s", human_readable (factors
[i
], buf
, 1, 1));
155 token_buffer tokenbuffer
;
157 init_tokenbuffer (&tokenbuffer
);
161 long int token_length
;
163 token_length
= readtoken (stdin
, DELIM
, sizeof (DELIM
) - 1,
165 if (token_length
< 0)
167 fail
|= print_factors (tokenbuffer
.buffer
);
169 free (tokenbuffer
.buffer
);
175 main (int argc
, char **argv
)
179 program_name
= argv
[0];
180 setlocale (LC_ALL
, "");
181 bindtextdomain (PACKAGE
, LOCALEDIR
);
182 textdomain (PACKAGE
);
184 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
186 /* The above handles --help and --version.
187 Since there is no other invocation of getopt, handle `--' here. */
188 if (argc
> 1 && STREQ (argv
[1], "--"))
200 for (i
= 1; i
< argc
; i
++)
201 fail
|= print_factors (argv
[i
]);