Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/factor Add -h option to factor(6): duplicate factors a...



details:   https://anonhg.NetBSD.org/src/rev/d82ed8ff3cde
branches:  trunk
changeset: 827761:d82ed8ff3cde
user:      rin <rin%NetBSD.org@localhost>
date:      Sat Nov 11 23:48:44 2017 +0000

description:
Add -h option to factor(6): duplicate factors are printed in
"human-readable" form of x^n.

diffstat:

 games/factor/factor.6 |  25 ++++++++++++++++++++-----
 games/factor/factor.c |  50 +++++++++++++++++++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 20 deletions(-)

diffs (210 lines):

diff -r 32ac3732e54e -r d82ed8ff3cde games/factor/factor.6
--- a/games/factor/factor.6     Sat Nov 11 21:05:58 2017 +0000
+++ b/games/factor/factor.6     Sat Nov 11 23:48:44 2017 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: factor.6,v 1.13 2014/10/02 21:36:37 ast Exp $
+.\"    $NetBSD: factor.6,v 1.14 2017/11/11 23:48:44 rin Exp $
 .\"
 .\" Copyright (c) 1989, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -35,7 +35,7 @@
 .\"
 .\" By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
 .\"
-.Dd May 15, 2010
+.Dd Nov 12, 2017
 .Dt FACTOR 6
 .Os
 .Sh NAME
@@ -43,6 +43,7 @@
 .Nd factor a number
 .Sh SYNOPSIS
 .Nm
+.Op Fl h
 .Op Ar number ...
 .Sh DESCRIPTION
 The
@@ -54,15 +55,20 @@
 .Pq prime
 factors on a single line.
 Factors are listed in ascending order, and are preceded by a space.
-If a factor divides a value more than once, it will be printed more than once.
+By default, if a factor divides a value more than once, it will be
+printed more than once.
 .Pp
 When
 .Nm
-is invoked with one or more arguments, each argument will be factored.
+is invoked with one or more
+.Ar number
+arguments, each argument will be factored.
 .Pp
 When
 .Nm
-is invoked with no arguments,
+is invoked with no
+.Ar number
+arguments,
 .Nm
 reads numbers, one per line, from standard input, until end of file or error.
 Leading white-space and empty lines are ignored.
@@ -84,6 +90,15 @@
 .Nm
 is compiled without OpenSSL it is limited to the maximum value of
 .Vt unsigned long .
+.Pp
+The following option is available:
+.Bl -tag -width flag
+.It Fl h
+If the
+.Fl h
+flag is specified, factors will be printed in "human-readable" format.
+If a factor x divides a value n (>1) times, it will appear as x^n.
+.El
 .Sh DIAGNOSTICS
 Out of range or invalid input results in
 an appropriate error message to standard error.
diff -r 32ac3732e54e -r d82ed8ff3cde games/factor/factor.c
--- a/games/factor/factor.c     Sat Nov 11 21:05:58 2017 +0000
+++ b/games/factor/factor.c     Sat Nov 11 23:48:44 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $  */
+/*     $NetBSD: factor.c,v 1.28 2017/11/11 23:48:44 rin Exp $  */
 
 /*
  * Copyright (c) 1989, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)factor.c   8.4 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $");
+__RCSID("$NetBSD: factor.c,v 1.28 2017/11/11 23:48:44 rin Exp $");
 #endif
 #endif /* not lint */
 
@@ -52,15 +52,18 @@
  * By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
  *
  * usage:
- *     factor [number] ...
+ *     factor [-h] [number] ...
  *
- * The form of the output is:
+ * By Default, the form of the output is:
  *
  *     number: factor1 factor1 factor2 factor3 factor3 factor3 ...
  *
  * where factor1 <= factor2 <= factor3 <= ...
  *
- * If no args are given, the list of numbers are read from stdin.
+ * If the -h flag is specified, the output is in "human-readable" format.
+ * Duplicate factors are printed in the form of x^n.
+ *
+ * If no number args are given, the list of numbers are read from stdin.
  */
 
 #include <ctype.h>
@@ -103,7 +106,7 @@
 static BN_CTX *ctx;                    /* just use a global context */
 #endif
 
-static void pr_fact(BIGNUM *);         /* print factors of a value */
+static void pr_fact(BIGNUM *, int);    /* print factors of a value */
 static void BN_print_dec_fp(FILE *, const BIGNUM *);
 static void usage(void) __dead;
 #ifdef HAVE_OPENSSL
@@ -132,7 +135,7 @@
 main(int argc, char *argv[])
 {
        BIGNUM *val;
-       int ch;
+       int ch, hflag;
        char *p, buf[LINE_MAX];         /* > max number of digits. */
 
 #ifdef HAVE_OPENSSL 
@@ -142,8 +145,12 @@
        if (val == NULL)
                errx(1, "can't initialise bignum");
 
-       while ((ch = getopt(argc, argv, "")) != -1)
+       hflag = 0;
+       while ((ch = getopt(argc, argv, "h")) != -1)
                switch (ch) {
+               case 'h':
+                       hflag = 1;
+                       break;
                case '?':
                default:
                        usage();
@@ -166,7 +173,7 @@
                                errx(1, "negative numbers aren't permitted.");
                        if (BN_dec2bn(&val, buf) == 0)
                                errx(1, "%s: illegal numeric format.", argv[0]);
-                       pr_fact(val);
+                       pr_fact(val, hflag);
                }
        /* Factor the arguments. */
        else
@@ -175,7 +182,7 @@
                                errx(1, "numbers <= 1 aren't permitted.");
                        if (BN_dec2bn(&val, argv[0]) == 0)
                                errx(1, "%s: illegal numeric format.", argv[0]);
-                       pr_fact(val);
+                       pr_fact(val, hflag);
                }
        exit(0);
 }
@@ -188,15 +195,19 @@
  * processing.
  *
  * Print the factors of the number, from the lowest to the highest.
- * A factor will be printed numtiple times if it divides the value
- * multiple times.
+ * By default, a factor will be printed numtiple times if it divides
+ * the value multiple times.
+ *
+ * If hflag is specified, duplicate factors are printed in "human-
+ * readable" form of x^n.
  *
  * Factors are printed with leading tabs.
  */
 static void
-pr_fact(BIGNUM *val)
+pr_fact(BIGNUM *val, int hflag)
 {
        const uint64_t *fact;           /* The factor found. */
+       int i;
 
        /* Firewall - catch 0 and 1. */
        if (BN_is_zero(val) || BN_is_one(val))
@@ -236,11 +247,20 @@
                }
 
                /* Divide factor out until none are left. */
+               i = 0;
                do {
-                       printf(" %" PRIu64, *fact);
+                       i++;
+                       if (!hflag)
+                               printf(" %" PRIu64, *fact);
                        BN_div_word(val, (BN_ULONG)*fact);
                } while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
 
+               if (hflag) {
+                       printf(" %" PRIu64, *fact);
+                       if (i > 1)
+                               printf("^%d", i);
+               }
+
                /* Let the user know we're doing something. */
                fflush(stdout);
        }
@@ -265,7 +285,7 @@
 void
 usage(void)
 {
-       fprintf(stderr, "usage: factor [value ...]\n");
+       fprintf(stderr, "usage: factor [-h] [value ...]\n");
        exit (0);
 }
 



Home | Main Index | Thread Index | Old Index