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