summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS5
-rw-r--r--THANKS.in1
-rw-r--r--doc/coreutils.texi24
-rw-r--r--src/factor.c29
-rwxr-xr-xtests/misc/factor.pl5
5 files changed, 49 insertions, 15 deletions
diff --git a/NEWS b/NEWS
index 3a9148637..16984003c 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,11 @@ GNU coreutils NEWS -*- outline -*-
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]
diff --git a/THANKS.in b/THANKS.in
index 311196a04..c67a64b88 100644
--- a/THANKS.in
+++ b/THANKS.in
@@ -188,6 +188,7 @@ Eivind eivindt@multinet.no
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
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index b1ec7c61c..7bca37b71 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -18619,26 +18619,30 @@ These programs do numerically-related operations.
@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
diff --git a/src/factor.c b/src/factor.c
index 66ce28b84..ca315c2b0 100644
--- a/src/factor.c
+++ b/src/factor.c
@@ -226,12 +226,16 @@ enum
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 */
@@ -2457,6 +2461,12 @@ print_factors_single (uintmax_t t1, uintmax_t t0)
{
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])
@@ -2525,6 +2535,11 @@ print_factors (char const *input)
{
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);
@@ -2542,15 +2557,17 @@ usage (int status)
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);
@@ -2593,10 +2610,14 @@ main (int argc, char **argv)
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;
diff --git a/tests/misc/factor.pl b/tests/misc/factor.pl
index a12768bf3..f399656c7 100755
--- a/tests/misc/factor.pl
+++ b/tests/misc/factor.pl
@@ -89,6 +89,8 @@ my @Tests =
['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'}],
);
@@ -99,7 +101,8 @@ my $t;
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)