2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
43 static const char rcsid[] =
48 * primes - generate a table of primes between two values
50 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
52 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
55 * primes [-h] [start [stop]]
57 * Print primes >= start and < stop. If stop is omitted,
58 * the value 4294967295 (2^32-1) is assumed. If start is
59 * omitted, start is read from standard input.
61 * validation check: there are 664579 primes between 0 and 10^7
77 * Eratosthenes sieve table
79 * We only sieve the odd numbers. The base of our sieve windows are always
80 * odd. If the base of table is 1, table[i] represents 2*i-1. After the
81 * sieve, table[i] == 1 if and only if 2*i-1 is prime.
83 * We make TABSIZE large to reduce the overhead of inner loop setup.
85 static char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */
89 static void primes(ubig, ubig);
90 static ubig read_num_buf(void);
91 static void usage(void);
94 main(int argc, char *argv[])
96 ubig start; /* where to start generating */
97 ubig stop; /* don't generate at or above this value */
101 while ((ch = getopt(argc, argv, "h")) != -1)
117 * Convert low and high args. Strtoul(3) sets errno to
118 * ERANGE if the number is too large, but, if there's
119 * a leading minus sign it returns the negation of the
120 * result of the conversion, which we'd rather disallow.
124 /* Start and stop supplied on the command line. */
125 if (argv[0][0] == '-' || argv[1][0] == '-')
126 errx(1, "negative numbers aren't permitted.");
129 start = strtoul(argv[0], &p, 0);
131 err(1, "%s", argv[0]);
133 errx(1, "%s: illegal numeric format.", argv[0]);
136 stop = strtoul(argv[1], &p, 0);
138 err(1, "%s", argv[1]);
140 errx(1, "%s: illegal numeric format.", argv[1]);
143 /* Start on the command line. */
144 if (argv[0][0] == '-')
145 errx(1, "negative numbers aren't permitted.");
148 start = strtoul(argv[0], &p, 0);
150 err(1, "%s", argv[0]);
152 errx(1, "%s: illegal numeric format.", argv[0]);
155 start = read_num_buf();
162 errx(1, "start value must be less than stop value.");
169 * This routine returns a number n, where 0 <= n && n <= BIG.
175 char *p, buf[LINE_MAX]; /* > max number of digits. */
178 if (fgets(buf, sizeof(buf), stdin) == NULL) {
183 for (p = buf; isblank(*p); ++p);
184 if (*p == '\n' || *p == '\0')
187 errx(1, "negative numbers aren't permitted.");
189 val = strtoul(buf, &p, 0);
193 errx(1, "%s: illegal numeric format.", buf);
199 * primes - sieve and print primes from start up to and but not including stop
202 primes(ubig start, ubig stop)
204 char *q; /* sieve spot */
205 ubig factor; /* index and factor */
206 char *tab_lim; /* the limit to sieve on the table */
207 const ubig *p; /* prime table pointer */
208 ubig fact_lim; /* highest prime for current block */
209 ubig mod; /* temp storage for mod */
212 * A number of systems can not convert double values into unsigned
213 * longs when the values are larger than the largest signed value.
214 * We don't have this problem, so we can go all the way to BIG.
227 * be sure that the values are odd, or 2
229 if (start != 2 && (start&0x1) == 0) {
232 if (stop != 2 && (stop&0x1) == 0) {
237 * quick list of primes <= pr_limit
239 if (start <= *pr_limit) {
240 /* skip primes up to the start value */
241 for (p = &prime[0], factor = prime[0];
242 factor < stop && p <= pr_limit; factor = *(++p)) {
243 if (factor >= start) {
244 printf(hflag ? "0x%lx\n" : "%lu\n", factor);
247 /* return early if we are done */
255 * we shall sieve a bytemap window, note primes and move the window
256 * upward until we pass the stop point
258 while (start < stop) {
260 * factor out 3, 5, 7, 11 and 13
262 /* initial pattern copy */
263 factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */
264 memcpy(table, &pattern[factor], pattern_size-factor);
265 /* main block pattern copies */
266 for (fact_lim=pattern_size-factor;
267 fact_lim+pattern_size<=TABSIZE; fact_lim+=pattern_size) {
268 memcpy(&table[fact_lim], pattern, pattern_size);
270 /* final block pattern copy */
271 memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim);
274 * sieve for primes 17 and higher
276 /* note highest useful factor and sieve spot */
277 if (stop-start > TABSIZE+TABSIZE) {
278 tab_lim = &table[TABSIZE]; /* sieve it all */
279 fact_lim = sqrt(start+1.0+TABSIZE+TABSIZE);
281 tab_lim = &table[(stop-start)/2]; /* partial sieve */
282 fact_lim = sqrt(stop+1.0);
284 /* sieve for factors >= 17 */
285 factor = 17; /* 17 is first prime to use */
286 p = &prime[7]; /* 19 is next prime, pi(19)=7 */
288 /* determine the factor's initial sieve point */
291 q = &table[(factor-mod)/2];
293 q = &table[mod ? factor-(mod/2) : 0];
295 /* sive for our current factor */
296 for ( ; q < tab_lim; q += factor) {
297 *q = '\0'; /* sieve out a spot */
300 } while (factor <= fact_lim);
303 * print generated primes
305 for (q = table; q < tab_lim; ++q, start+=2) {
307 printf(hflag ? "0x%lx\n" : "%lu\n", start);
316 fprintf(stderr, "usage: primes [-h] [start [stop]]\n");