chiark / gitweb /
math/mpx-mul4-*-sse2.S (mpxmont_redc4): Fix end-of-outer-loop commentary.
[catacomb] / progs / factorial.c
1 /* -*-c-*-
2  *
3  * Example factorial computation
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <ctype.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <mLib/macros.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
41
42 #include "mpint.h"
43 #include "mpmul.h"
44 #include "mptext.h"
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 static void usage(FILE *fp)
49 {
50   pquis(fp, "Usage: $ [-r RADIX] INTEGER\n");
51 }
52
53 static void version(FILE *fp)
54 {
55   pquis(fp, "$, Catacomb version " VERSION "\n");
56 }
57
58 static void help(FILE *fp)
59 {
60   version(fp);
61   putc('\n', fp);
62   usage(fp);
63   fputs("\n\
64 Prints the factorial of the given integer on its output.  Input may be\n\
65 in decimal (the default), octal with preceding zero, hex with preceding\n\
66 `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
67 Output may be in any base between 2 and 62; the default is base 10.  For\n\
68 bases between 11 and 36 inclusive, lowercase letters of either case are\n\
69 used as additional digits with values 10 upwards; lowercase is always\n\
70 used for output.  For bases between 37 and 62 inclusive, lowercase letters\n\
71 have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
72 the value 36.\n\
73 \n\
74 Options provided:\n\
75 \n\
76 -h, --help              Display this help message.\n\
77 -v, --version           Display the version number.\n\
78 -u, --usage             Display a usage message.\n\
79 \n\
80 -r, --radix=N           Write output in base N.\n\
81 ", fp);
82 }
83
84 int main(int argc, char *argv[])
85 {
86   unsigned long x;
87   int r = 10;
88   char *p;
89   mp *f, *ulmax, *xx;
90   unsigned fl = 0;
91
92 #define f_bogus 1u
93
94   ego(argv[0]);
95
96   for (;;) {
97     static const struct option opt[] = {
98       { "help",         0,              0,      'h' },
99       { "version",      0,              0,      'v' },
100       { "usage",        0,              0,      'u' },
101       { "radix",        OPTF_ARGREQ,    0,      'r' },
102       { 0,              0,              0,      0 }
103     };
104     int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
105     if (i < 0)
106       break;
107     switch (i) {
108       case 'h':
109         help(stdout);
110         exit(0);
111       case 'v':
112         version(stdout);
113         exit(0);
114       case 'u':
115         usage(stdout);
116         exit(0);
117       case 'r':
118         r = atoi(optarg);
119         if (r < 2 || r > 62)
120           die(EXIT_FAILURE, "bad radix `%s'", optarg);
121         break;
122       default:
123         fl |= f_bogus;
124         break;
125     }
126   }
127
128   if (optind + 1 != argc || (fl & f_bogus)) {
129     usage(stderr);
130     exit(EXIT_FAILURE);
131   }
132   ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
133   p = argv[optind];
134   while (ISSPACE(*p))
135     p++;
136   xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
137   while (ISSPACE(*p))
138     p++;
139   if (!xx || *p || MP_CMP(xx, <, MP_ZERO) || MP_CMP(xx, >, ulmax))
140     die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
141   x = mp_toulong(xx);
142   mp_drop(xx);
143   mp_drop(ulmax);
144   f = mp_factorial(x);
145   mp_writefile(f, stdout, r);
146   fputc('\n', stdout);
147   mp_drop(f);
148   return (0);
149 }
150
151 /*----- That's all, folks -------------------------------------------------*/