primes.c revision 1.22 1 1.22 christos /* $NetBSD: primes.c,v 1.22 2018/02/03 15:40:29 christos Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.4 cgd * Copyright (c) 1989, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Landon Curt Noll.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.11 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.7 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.16 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 1.16 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.4 cgd #if 0
43 1.6 tls static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
44 1.4 cgd #else
45 1.22 christos __RCSID("$NetBSD: primes.c,v 1.22 2018/02/03 15:40:29 christos Exp $");
46 1.4 cgd #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * primes - generate a table of primes between two values
51 1.1 cgd *
52 1.20 ast * By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
53 1.1 cgd *
54 1.1 cgd * usage:
55 1.21 wiz * primes [-dh] [start [stop]]
56 1.1 cgd *
57 1.1 cgd * Print primes >= start and < stop. If stop is omitted,
58 1.20 ast * the value SPSPMAX is assumed. If start is
59 1.1 cgd * omitted, start is read from standard input.
60 1.21 wiz * -d: print difference to previous prime, e.g. 3 (1)
61 1.20 ast * -h: print primes in hexadecimal
62 1.1 cgd *
63 1.1 cgd * validation check: there are 664579 primes between 0 and 10^7
64 1.1 cgd */
65 1.1 cgd
66 1.4 cgd #include <ctype.h>
67 1.4 cgd #include <err.h>
68 1.4 cgd #include <errno.h>
69 1.20 ast #include <inttypes.h>
70 1.4 cgd #include <limits.h>
71 1.1 cgd #include <math.h>
72 1.4 cgd #include <stdio.h>
73 1.4 cgd #include <stdlib.h>
74 1.20 ast #include <string.h>
75 1.6 tls #include <unistd.h>
76 1.4 cgd
77 1.1 cgd #include "primes.h"
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * Eratosthenes sieve table
81 1.1 cgd *
82 1.1 cgd * We only sieve the odd numbers. The base of our sieve windows are always
83 1.1 cgd * odd. If the base of table is 1, table[i] represents 2*i-1. After the
84 1.20 ast * sieve, table[i] == 1 if and only if 2*i-1 is prime.
85 1.1 cgd *
86 1.1 cgd * We make TABSIZE large to reduce the overhead of inner loop setup.
87 1.1 cgd */
88 1.17 dholland static char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */
89 1.1 cgd
90 1.20 ast static int dflag, hflag;
91 1.1 cgd
92 1.20 ast static void primes(uint64_t, uint64_t);
93 1.20 ast static uint64_t read_num_buf(void);
94 1.20 ast static void usage(void) __dead;
95 1.1 cgd
96 1.1 cgd
97 1.4 cgd int
98 1.15 matt main(int argc, char *argv[])
99 1.1 cgd {
100 1.20 ast uint64_t start; /* where to start generating */
101 1.20 ast uint64_t stop; /* don't generate at or above this value */
102 1.4 cgd int ch;
103 1.4 cgd char *p;
104 1.4 cgd
105 1.20 ast while ((ch = getopt(argc, argv, "dh")) != -1)
106 1.4 cgd switch (ch) {
107 1.15 matt case 'd':
108 1.15 matt dflag++;
109 1.15 matt break;
110 1.20 ast case 'h':
111 1.20 ast hflag++;
112 1.20 ast break;
113 1.4 cgd case '?':
114 1.4 cgd default:
115 1.4 cgd usage();
116 1.4 cgd }
117 1.4 cgd argc -= optind;
118 1.4 cgd argv += optind;
119 1.1 cgd
120 1.1 cgd start = 0;
121 1.22 christos stop = (uint64_t)(-1);
122 1.1 cgd
123 1.4 cgd /*
124 1.20 ast * Convert low and high args. Strtoumax(3) sets errno to
125 1.4 cgd * ERANGE if the number is too large, but, if there's
126 1.4 cgd * a leading minus sign it returns the negation of the
127 1.4 cgd * result of the conversion, which we'd rather disallow.
128 1.4 cgd */
129 1.4 cgd switch (argc) {
130 1.4 cgd case 2:
131 1.4 cgd /* Start and stop supplied on the command line. */
132 1.4 cgd if (argv[0][0] == '-' || argv[1][0] == '-')
133 1.4 cgd errx(1, "negative numbers aren't permitted.");
134 1.4 cgd
135 1.4 cgd errno = 0;
136 1.20 ast start = strtoumax(argv[0], &p, 0);
137 1.4 cgd if (errno)
138 1.4 cgd err(1, "%s", argv[0]);
139 1.4 cgd if (*p != '\0')
140 1.4 cgd errx(1, "%s: illegal numeric format.", argv[0]);
141 1.4 cgd
142 1.4 cgd errno = 0;
143 1.20 ast stop = strtoumax(argv[1], &p, 0);
144 1.4 cgd if (errno)
145 1.4 cgd err(1, "%s", argv[1]);
146 1.4 cgd if (*p != '\0')
147 1.4 cgd errx(1, "%s: illegal numeric format.", argv[1]);
148 1.4 cgd break;
149 1.4 cgd case 1:
150 1.4 cgd /* Start on the command line. */
151 1.4 cgd if (argv[0][0] == '-')
152 1.4 cgd errx(1, "negative numbers aren't permitted.");
153 1.4 cgd
154 1.4 cgd errno = 0;
155 1.20 ast start = strtoumax(argv[0], &p, 0);
156 1.4 cgd if (errno)
157 1.4 cgd err(1, "%s", argv[0]);
158 1.4 cgd if (*p != '\0')
159 1.4 cgd errx(1, "%s: illegal numeric format.", argv[0]);
160 1.4 cgd break;
161 1.4 cgd case 0:
162 1.4 cgd start = read_num_buf();
163 1.4 cgd break;
164 1.4 cgd default:
165 1.4 cgd usage();
166 1.4 cgd }
167 1.1 cgd
168 1.4 cgd if (start > stop)
169 1.4 cgd errx(1, "start value must be less than stop value.");
170 1.1 cgd primes(start, stop);
171 1.20 ast return (0);
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd /*
175 1.4 cgd * read_num_buf --
176 1.20 ast * This routine returns a number n, where 0 <= n && n <= ULONG_MAX.
177 1.1 cgd */
178 1.20 ast static uint64_t
179 1.15 matt read_num_buf(void)
180 1.1 cgd {
181 1.20 ast uint64_t val;
182 1.20 ast char *p, buf[LINE_MAX]; /* > max number of digits. */
183 1.1 cgd
184 1.4 cgd for (;;) {
185 1.4 cgd if (fgets(buf, sizeof(buf), stdin) == NULL) {
186 1.4 cgd if (ferror(stdin))
187 1.4 cgd err(1, "stdin");
188 1.4 cgd exit(0);
189 1.1 cgd }
190 1.18 tnozaki for (p = buf; isblank((unsigned char)*p); ++p);
191 1.4 cgd if (*p == '\n' || *p == '\0')
192 1.1 cgd continue;
193 1.4 cgd if (*p == '-')
194 1.4 cgd errx(1, "negative numbers aren't permitted.");
195 1.4 cgd errno = 0;
196 1.20 ast val = strtoumax(buf, &p, 0);
197 1.4 cgd if (errno)
198 1.4 cgd err(1, "%s", buf);
199 1.4 cgd if (*p != '\n')
200 1.4 cgd errx(1, "%s: illegal numeric format.", buf);
201 1.4 cgd return (val);
202 1.4 cgd }
203 1.1 cgd }
204 1.1 cgd
205 1.1 cgd /*
206 1.1 cgd * primes - sieve and print primes from start up to and but not including stop
207 1.1 cgd */
208 1.20 ast static void
209 1.20 ast primes(uint64_t start, uint64_t stop)
210 1.1 cgd {
211 1.7 lukem char *q; /* sieve spot */
212 1.20 ast uint64_t factor; /* index and factor */
213 1.7 lukem char *tab_lim; /* the limit to sieve on the table */
214 1.20 ast const uint64_t *p; /* prime table pointer */
215 1.20 ast uint64_t fact_lim; /* highest prime for current block */
216 1.20 ast uint64_t mod; /* temp storage for mod */
217 1.20 ast uint64_t prev = 0;
218 1.1 cgd
219 1.1 cgd /*
220 1.4 cgd * A number of systems can not convert double values into unsigned
221 1.4 cgd * longs when the values are larger than the largest signed value.
222 1.20 ast * We don't have this problem, so we can go all the way to ULONG_MAX.
223 1.1 cgd */
224 1.1 cgd if (start < 3) {
225 1.20 ast start = 2;
226 1.1 cgd }
227 1.1 cgd if (stop < 3) {
228 1.20 ast stop = 2;
229 1.1 cgd }
230 1.1 cgd if (stop <= start) {
231 1.1 cgd return;
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd /*
235 1.1 cgd * be sure that the values are odd, or 2
236 1.1 cgd */
237 1.1 cgd if (start != 2 && (start&0x1) == 0) {
238 1.1 cgd ++start;
239 1.1 cgd }
240 1.1 cgd if (stop != 2 && (stop&0x1) == 0) {
241 1.1 cgd ++stop;
242 1.1 cgd }
243 1.1 cgd
244 1.1 cgd /*
245 1.1 cgd * quick list of primes <= pr_limit
246 1.1 cgd */
247 1.1 cgd if (start <= *pr_limit) {
248 1.1 cgd /* skip primes up to the start value */
249 1.1 cgd for (p = &prime[0], factor = prime[0];
250 1.4 cgd factor < stop && p <= pr_limit; factor = *(++p)) {
251 1.1 cgd if (factor >= start) {
252 1.20 ast printf(hflag ? "%" PRIx64 : "%" PRIu64, factor);
253 1.15 matt if (dflag) {
254 1.20 ast printf(" (%" PRIu64 ")", factor - prev);
255 1.15 matt }
256 1.15 matt putchar('\n');
257 1.1 cgd }
258 1.15 matt prev = factor;
259 1.1 cgd }
260 1.1 cgd /* return early if we are done */
261 1.1 cgd if (p <= pr_limit) {
262 1.1 cgd return;
263 1.1 cgd }
264 1.1 cgd start = *pr_limit+2;
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * we shall sieve a bytemap window, note primes and move the window
269 1.1 cgd * upward until we pass the stop point
270 1.1 cgd */
271 1.1 cgd while (start < stop) {
272 1.1 cgd /*
273 1.1 cgd * factor out 3, 5, 7, 11 and 13
274 1.1 cgd */
275 1.1 cgd /* initial pattern copy */
276 1.1 cgd factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */
277 1.1 cgd memcpy(table, &pattern[factor], pattern_size-factor);
278 1.1 cgd /* main block pattern copies */
279 1.1 cgd for (fact_lim=pattern_size-factor;
280 1.4 cgd fact_lim+pattern_size<=TABSIZE; fact_lim+=pattern_size) {
281 1.1 cgd memcpy(&table[fact_lim], pattern, pattern_size);
282 1.1 cgd }
283 1.1 cgd /* final block pattern copy */
284 1.1 cgd memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim);
285 1.1 cgd
286 1.1 cgd /*
287 1.1 cgd * sieve for primes 17 and higher
288 1.1 cgd */
289 1.1 cgd /* note highest useful factor and sieve spot */
290 1.1 cgd if (stop-start > TABSIZE+TABSIZE) {
291 1.1 cgd tab_lim = &table[TABSIZE]; /* sieve it all */
292 1.20 ast fact_lim = sqrt(start+1.0+TABSIZE+TABSIZE);
293 1.1 cgd } else {
294 1.1 cgd tab_lim = &table[(stop-start)/2]; /* partial sieve */
295 1.20 ast fact_lim = sqrt(stop+1.0);
296 1.1 cgd }
297 1.1 cgd /* sieve for factors >= 17 */
298 1.1 cgd factor = 17; /* 17 is first prime to use */
299 1.1 cgd p = &prime[7]; /* 19 is next prime, pi(19)=7 */
300 1.1 cgd do {
301 1.1 cgd /* determine the factor's initial sieve point */
302 1.10 itojun mod = start%factor;
303 1.10 itojun if (mod & 0x1) {
304 1.10 itojun q = &table[(factor-mod)/2];
305 1.1 cgd } else {
306 1.10 itojun q = &table[mod ? factor-(mod/2) : 0];
307 1.1 cgd }
308 1.14 matt /* sieve for our current factor */
309 1.1 cgd for ( ; q < tab_lim; q += factor) {
310 1.1 cgd *q = '\0'; /* sieve out a spot */
311 1.1 cgd }
312 1.20 ast factor = *p++;
313 1.20 ast } while (factor <= fact_lim);
314 1.1 cgd
315 1.1 cgd /*
316 1.1 cgd * print generated primes
317 1.1 cgd */
318 1.1 cgd for (q = table; q < tab_lim; ++q, start+=2) {
319 1.1 cgd if (*q) {
320 1.20 ast if (start > SIEVEMAX) {
321 1.20 ast if (!isprime(start))
322 1.20 ast continue;
323 1.20 ast }
324 1.20 ast printf(hflag ? "%" PRIx64 : "%" PRIu64, start);
325 1.20 ast if (dflag && (prev || (start <= *pr_limit))) {
326 1.20 ast printf(" (%" PRIu64 ")", start - prev);
327 1.15 matt }
328 1.15 matt putchar('\n');
329 1.20 ast prev = start;
330 1.1 cgd }
331 1.1 cgd }
332 1.1 cgd }
333 1.4 cgd }
334 1.4 cgd
335 1.20 ast static void
336 1.15 matt usage(void)
337 1.4 cgd {
338 1.21 wiz (void)fprintf(stderr, "usage: primes [-dh] [start [stop]]\n");
339 1.4 cgd exit(1);
340 1.1 cgd }
341