]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: --exponents: new option for printing in p^e format
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Wed, 27 Apr 2022 10:07:20 +0000 (12:07 +0200)
committerPádraig Brady <P@draigBrady.com>
Mon, 9 May 2022 13:38:01 +0000 (14:38 +0100)
When factoring numbers that have a large 2^n factor, it can be hard to
eyeball just how many 2's there are. Add an option to print each prime
power factor in the p^e format (omitting the exponent when it is 1).

* src/factor.c: Add -h, --exponents option for printing in p^e format.
* doc/coreutils.texi (factor invocation): Document the new option.
* tests/misc/factor.pl: Add test case.
* THANKS.in: Add previous suggester
(https://lists.gnu.org/r/coreutils/2017-11/msg00015.html).

Suggested-by: Emanuel Landeholm <emanuel.landeholm@gmail.com>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
NEWS
THANKS.in
doc/coreutils.texi
src/factor.c
tests/misc/factor.pl

diff --git a/NEWS b/NEWS
index 3a9148637424e28caeb4b11bc4b124081908356d..16984003c08ee6dcd8a9e3b6f22de4d6e64edeab 100644 (file)
--- 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]
 
index 311196a0460eb5550e802647227e0af833933cf6..c67a64b884199d62535319b62b1cf7a4e0971d19 100644 (file)
--- 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
index b1ec7c61c15d38036b2408e0be5361df55f2d308..7bca37b71bcc594d14b580c7fef7eb8ec91efc2d 100644 (file)
@@ -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
index 66ce28b8436cf2b2a164c59f95ef6889a473f870..ca315c2b0816e852c8fd53cbc958daa5f620d9cc 100644 (file)
@@ -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;
index a12768bf34b1419c2dbdbef561a612f7e811ebaa..f399656c7fcd87db6a3e32d65e9956751020c2c9 100755 (executable)
@@ -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)