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