long been documented to be platform-dependent.
[bug introduced 1999-05-02 and only partly fixed in coreutils-8.14]
+** New Features
+
+ factor now accepts the --exponents (-h) option to print factors
+ in the form p^e, rather than repeating the prime p, e times.
+
* Noteworthy changes in release 9.1 (2022-04-15) [stable]
Elbert Pol elbert.pol@gmail.com
Eldon Stegall eldon@eldondev.com
Eli Zaretskii eliz@is.elta.co.il
+Emanuel Landeholm emanuel.landeholm@gmail.com
Emile LeBlanc leblanc@math.toronto.edu
Emmanuel Lacour elacour@home-dn.net
Eric Backus ericb@lsid.hp.com
@pindex factor
@cindex prime factors
-@command{factor} prints prime factors. Synopses:
+@command{factor} prints prime factors. Synopsis:
@example
-factor [@var{number}]@dots{}
-factor @var{option}
+factor [@var{option}]@dots{} [@var{number}]@dots{}
@end example
If no @var{number} is specified on the command line, @command{factor} reads
numbers from standard input, delimited by newlines, tabs, or spaces.
-The @command{factor} command supports only a small number of options:
+The program accepts the following options. Also see @ref{Common options}.
@table @samp
-@item --help
-Print a short help on standard output, then exit without further
-processing.
+@item -h
+@itemx --exponents
+@opindex -h
+@opindex --exponents
+print factors in the form @math{p^e}, rather than repeating
+the prime @samp{p}, @samp{e} times. If the exponent @samp{e} is 1,
+then it is omitted.
-@item --version
-Print the program version on standard output, then exit without further
-processing.
+@example
+$ factor --exponents 3000
+3000: 2^3 3 5^3
+@end example
@end table
If the number to be factored is small (less than @math{2^{127}} on
static struct option const long_options[] =
{
+ {"exponents", no_argument, NULL, 'h'},
{"-debug", no_argument, NULL, DEV_DEBUG_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
+/* If true, use p^e output format. */
+static bool print_exponents;
+
struct factors
{
uintmax_t plarge[2]; /* Can have a single large factor */
{
lbuf_putc (' ');
print_uintmaxes (0, factors.p[j]);
+ if (print_exponents && factors.e[j] > 1)
+ {
+ lbuf_putc ('^');
+ lbuf_putint (factors.e[j], 0);
+ break;
+ }
}
if (factors.plarge[1])
{
putchar (' ');
mpz_out_str (stdout, 10, factors.p[j]);
+ if (print_exponents && factors.e[j] > 1)
+ {
+ printf ("^%lu", factors.e[j]);
+ break;
+ }
}
mp_factor_clear (&factors);
else
{
printf (_("\
-Usage: %s [NUMBER]...\n\
- or: %s OPTION\n\
+Usage: %s [OPTION] [NUMBER]...\n\
"),
- program_name, program_name);
+ program_name);
fputs (_("\
Print the prime factors of each specified integer NUMBER. If none\n\
are specified on the command line, read them from standard input.\n\
\n\
"), stdout);
+ fputs ("\
+ -h, --exponents print repeated factors in form p^e unless e is 1\n\
+", stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
emit_ancillary_info (PROGRAM_NAME);
atexit (lbuf_flush);
int c;
- while ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1)
+ while ((c = getopt_long (argc, argv, "h", long_options, NULL)) != -1)
{
switch (c)
{
+ case 'h': /* NetBSD used -h for this functionality first. */
+ print_exponents = true;
+ break;
+
case DEV_DEBUG_OPTION:
dev_debug = true;
break;
['bug-gmp-plus_2_sup_128_plus_1',
'+170141183460469231731687303715884105729',
{OUT => '3 56713727820156410577229101238628035243'}],
+ ['h-1', '-h 3000', {OUT => '2^3 3 5^3'}],
+ ['h-2', '3000 --exponents', {OUT => '2^3 3 5^3'}],
);
Test:
foreach $t (@Tests)
{
- (my $arg1 = $t->[1]) =~ s| *\+?||;
+ (my $arg1 = $t->[1]) =~ s| *\+?||; # strip '+'
+ (my $arg1 = $arg1) =~ s| *-[^ ]+ *||; # strip option
# Don't fiddle with expected OUT string if there's a nonzero exit status.
foreach my $e (@$t)