List,

I'm working with large composite numbers and would like to have the option
of printing the factorization in exponential form. I have attached a patch
for printing factorizations in this form:

    $ src/factor
    68000
    68000: 2^5 5^3 17

I'm no expert on GNU coding standards so my patch is probably not 100%
kosher. Also, I haven't figured out how to make long_options[] work (is
this code path a NOP on non-debug builds?) so my patch simply hard codes
exp_form to true. Maybe some of the devs could massage my patch into
something more workable.

Cheers and thanks in advance!
diff --git a/src/factor.c b/src/factor.c
index 97af6caae..e0374363f 100644
--- a/src/factor.c
+++ b/src/factor.c
@@ -227,12 +227,14 @@ const unsigned char factor_clz_tab[129] =
 
 enum
 {
+  EXP_FORM_OPTION,
   DEV_DEBUG_OPTION = CHAR_MAX + 1
 };
 
 static struct option const long_options[] =
 {
   {"-debug", no_argument, NULL, DEV_DEBUG_OPTION},
+  {"-exp", no_argument, NULL, EXP_FORM_OPTION},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -706,6 +708,10 @@ verify (W <= WIDE_UINT_BITS);
    This flag is used only in the GMP code.  */
 static bool dev_debug = false;
 
+/* display prime factors in exponential form */
+
+static bool exp_form = true;
+
 /* Prove primality or run probabilistic tests.  */
 static bool flag_prove_primality = PROVE_PRIMALITY;
 
@@ -2475,11 +2481,23 @@ print_factors_single (uintmax_t t1, uintmax_t t0)
   factor (t1, t0, &factors);
 
   for (unsigned int j = 0; j < factors.nfactors; j++)
-    for (unsigned int k = 0; k < factors.e[j]; k++)
+    {
+      if (exp_form && factors.e[j] > 1)
       {
         lbuf_putc (' ');
         print_uintmaxes (0, factors.p[j]);
+        lbuf_putc ('^');
+        lbuf_putint (factors.e[j], 0);
+      }
+      else
+      {
+        for (unsigned int k = 0; k < factors.e[j]; k++)
+          {
+            lbuf_putc (' ');
+            print_uintmaxes (0, factors.p[j]);
+          }
       }
+    }
 
   if (factors.plarge[1])
     {
@@ -2526,6 +2544,8 @@ print_factors (const char *input)
       return false;
     }
 
+  puts("kaka!");
+
 #if HAVE_GMP
   devmsg ("[using arbitrary-precision arithmetic] ");
   mpz_t t;
@@ -2537,8 +2557,18 @@ print_factors (const char *input)
   mp_factor (t, &factors);
 
   for (unsigned int j = 0; j < factors.nfactors; j++)
-    for (unsigned int k = 0; k < factors.e[j]; k++)
-      gmp_printf (" %Zd", factors.p[j]);
+    {
+      if (exp_form && factors.e[j] > 1)
+        {
+          gmp_printf (" %Zd", factors.p[j]);
+          printf("^%lud", factors.e[j]);
+        }
+      else
+        {
+          for (unsigned int k = 0; k < factors.e[j]; k++)
+            gmp_printf (" %Zd", factors.p[j]);
+        }
+    }
 
   mp_factor_clear (&factors);
   mpz_clear (t);
@@ -2618,6 +2648,10 @@ main (int argc, char **argv)
           dev_debug = true;
           break;
 
+        case EXP_FORM_OPTION:
+          exp_form = true;
+          break;
+
         case_GETOPT_HELP_CHAR;
 
         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);

Reply via email to