121199Sdist /*
260833Sbostic * Copyright (c) 1989, 1993
360833Sbostic * The Regents of the University of California. All rights reserved.
439969Sbostic *
539969Sbostic * This code is derived from software contributed to Berkeley by
639969Sbostic * Landon Curt Noll.
739969Sbostic *
842586Sbostic * %sccs.include.redist.c%
921199Sdist */
1021199Sdist
1113070Ssam #ifndef lint
1260833Sbostic static char copyright[] =
1360833Sbostic "@(#) Copyright (c) 1989, 1993\n\
1460833Sbostic The Regents of the University of California. All rights reserved.\n";
1539969Sbostic #endif /* not lint */
1613070Ssam
1721199Sdist #ifndef lint
18*69333Sbostic static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 05/10/95";
1939969Sbostic #endif /* not lint */
2021199Sdist
2113070Ssam /*
2239969Sbostic * primes - generate a table of primes between two values
2313070Ssam *
2466292Sbostic * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
2513070Ssam *
2666292Sbostic * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
2713070Ssam *
2839969Sbostic * usage:
2939969Sbostic * primes [start [stop]]
3039969Sbostic *
3139981Sbostic * Print primes >= start and < stop. If stop is omitted,
3239969Sbostic * the value 4294967295 (2^32-1) is assumed. If start is
3339969Sbostic * omitted, start is read from standard input.
3439969Sbostic *
3539981Sbostic * validation check: there are 664579 primes between 0 and 10^7
3613070Ssam */
3713070Ssam
3866410Sbostic #include <ctype.h>
3966292Sbostic #include <err.h>
4066292Sbostic #include <errno.h>
4166292Sbostic #include <limits.h>
4213070Ssam #include <math.h>
4339969Sbostic #include <memory.h>
4466292Sbostic #include <stdio.h>
4566292Sbostic #include <stdlib.h>
46*69333Sbostic #include <unistd.h>
4766410Sbostic
4839969Sbostic #include "primes.h"
4913070Ssam
5039969Sbostic /*
5139969Sbostic * Eratosthenes sieve table
5239969Sbostic *
5339969Sbostic * We only sieve the odd numbers. The base of our sieve windows are always
5439969Sbostic * odd. If the base of table is 1, table[i] represents 2*i-1. After the
5539969Sbostic * sieve, table[i] == 1 if and only iff 2*i-1 is prime.
5639969Sbostic *
5739969Sbostic * We make TABSIZE large to reduce the overhead of inner loop setup.
5839969Sbostic */
5939969Sbostic char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */
6013070Ssam
6139969Sbostic /*
6239969Sbostic * prime[i] is the (i-1)th prime.
6339969Sbostic *
6439969Sbostic * We are able to sieve 2^32-1 because this byte table yields all primes
6539969Sbostic * up to 65537 and 65537^2 > 2^32-1.
6639969Sbostic */
6739969Sbostic extern ubig prime[];
6839969Sbostic extern ubig *pr_limit; /* largest prime in the prime array */
6913070Ssam
7039969Sbostic /*
7139969Sbostic * To avoid excessive sieves for small factors, we use the table below to
7239969Sbostic * setup our sieve blocks. Each element represents a odd number starting
7339969Sbostic * with 1. All non-zero elements are factors of 3, 5, 7, 11 and 13.
7439969Sbostic */
7539969Sbostic extern char pattern[];
7639969Sbostic extern int pattern_size; /* length of pattern array */
7713070Ssam
7866410Sbostic void primes __P((ubig, ubig));
7966410Sbostic ubig read_num_buf __P((void));
8066410Sbostic void usage __P((void));
8113070Ssam
8266292Sbostic int
main(argc,argv)8313070Ssam main(argc, argv)
8466292Sbostic int argc;
8566292Sbostic char *argv[];
8613070Ssam {
8766292Sbostic ubig start; /* where to start generating */
8866292Sbostic ubig stop; /* don't generate at or above this value */
8966292Sbostic int ch;
9066410Sbostic char *p;
9113070Ssam
9266292Sbostic while ((ch = getopt(argc, argv, "")) != EOF)
9366292Sbostic switch (ch) {
9466292Sbostic case '?':
9566292Sbostic default:
9666292Sbostic usage();
9766292Sbostic }
9866292Sbostic argc -= optind;
9966292Sbostic argv += optind;
10066292Sbostic
10166410Sbostic start = 0;
10266410Sbostic stop = BIG;
10366410Sbostic
10439969Sbostic /*
10566410Sbostic * Convert low and high args. Strtoul(3) sets errno to
10666410Sbostic * ERANGE if the number is too large, but, if there's
10766410Sbostic * a leading minus sign it returns the negation of the
10866410Sbostic * result of the conversion, which we'd rather disallow.
10939969Sbostic */
11066410Sbostic switch (argc) {
11166410Sbostic case 2:
11266410Sbostic /* Start and stop supplied on the command line. */
11366410Sbostic if (argv[0][0] == '-' || argv[1][0] == '-')
11466410Sbostic errx(1, "negative numbers aren't permitted.");
11566410Sbostic
11666410Sbostic errno = 0;
11766410Sbostic start = strtoul(argv[0], &p, 10);
11866410Sbostic if (errno)
11966292Sbostic err(1, "%s", argv[0]);
12066410Sbostic if (*p != '\0')
12166410Sbostic errx(1, "%s: illegal numeric format.", argv[0]);
12266410Sbostic
12366410Sbostic errno = 0;
12466410Sbostic stop = strtoul(argv[1], &p, 10);
12566410Sbostic if (errno)
12666410Sbostic err(1, "%s", argv[1]);
12766410Sbostic if (*p != '\0')
12866410Sbostic errx(1, "%s: illegal numeric format.", argv[1]);
12966410Sbostic break;
13066410Sbostic case 1:
13166410Sbostic /* Start on the command line. */
13266410Sbostic if (argv[0][0] == '-')
13366410Sbostic errx(1, "negative numbers aren't permitted.");
13466410Sbostic
13566410Sbostic errno = 0;
13666410Sbostic start = strtoul(argv[0], &p, 10);
13766410Sbostic if (errno)
13866292Sbostic err(1, "%s", argv[0]);
13966410Sbostic if (*p != '\0')
14066410Sbostic errx(1, "%s: illegal numeric format.", argv[0]);
14166410Sbostic break;
14266410Sbostic case 0:
14366410Sbostic start = read_num_buf();
14466410Sbostic break;
14566410Sbostic default:
14666410Sbostic usage();
14713070Ssam }
14866410Sbostic
14966292Sbostic if (start > stop)
15066410Sbostic errx(1, "start value must be less than stop value.");
15139969Sbostic primes(start, stop);
15239969Sbostic exit(0);
15339969Sbostic }
15413070Ssam
15539969Sbostic /*
15666410Sbostic * read_num_buf --
15766410Sbostic * This routine returns a number n, where 0 <= n && n <= BIG.
15839969Sbostic */
15966410Sbostic ubig
read_num_buf()16066410Sbostic read_num_buf()
16139969Sbostic {
16266410Sbostic ubig val;
16366410Sbostic char *p, buf[100]; /* > max number of digits. */
16439969Sbostic
16566410Sbostic for (;;) {
16666410Sbostic if (fgets(buf, sizeof(buf), stdin) == NULL) {
16766410Sbostic if (ferror(stdin))
16866410Sbostic err(1, "stdin");
16966411Sbostic exit(0);
17039969Sbostic }
17166410Sbostic for (p = buf; isblank(*p); ++p);
17266410Sbostic if (*p == '\n' || *p == '\0')
17339969Sbostic continue;
17466410Sbostic if (*p == '-')
17566410Sbostic errx(1, "negative numbers aren't permitted.");
17666410Sbostic errno = 0;
17766410Sbostic val = strtoul(buf, &p, 10);
17866410Sbostic if (errno)
17966410Sbostic err(1, "%s", buf);
18066410Sbostic if (*p != '\n')
18166410Sbostic errx(1, "%s: illegal numeric format.", buf);
18266410Sbostic return (val);
18366410Sbostic }
18439969Sbostic }
18539969Sbostic
18613070Ssam /*
18739969Sbostic * primes - sieve and print primes from start up to and but not including stop
18813070Ssam */
18939969Sbostic void
primes(start,stop)19039969Sbostic primes(start, stop)
19139969Sbostic ubig start; /* where to start generating */
19239969Sbostic ubig stop; /* don't generate at or above this value */
19339969Sbostic {
19439969Sbostic register char *q; /* sieve spot */
19539969Sbostic register ubig factor; /* index and factor */
19639969Sbostic register char *tab_lim; /* the limit to sieve on the table */
19739969Sbostic register ubig *p; /* prime table pointer */
19839969Sbostic register ubig fact_lim; /* highest prime for current block */
19939969Sbostic
20013070Ssam /*
20166292Sbostic * A number of systems can not convert double values into unsigned
20266292Sbostic * longs when the values are larger than the largest signed value.
20366292Sbostic * We don't have this problem, so we can go all the way to BIG.
20413070Ssam */
20539969Sbostic if (start < 3) {
20639969Sbostic start = (ubig)2;
20739969Sbostic }
20839969Sbostic if (stop < 3) {
20939969Sbostic stop = (ubig)2;
21039969Sbostic }
21139969Sbostic if (stop <= start) {
21239969Sbostic return;
21339969Sbostic }
21439969Sbostic
21513070Ssam /*
21639969Sbostic * be sure that the values are odd, or 2
21713070Ssam */
21839969Sbostic if (start != 2 && (start&0x1) == 0) {
21939969Sbostic ++start;
22013070Ssam }
22139969Sbostic if (stop != 2 && (stop&0x1) == 0) {
22239969Sbostic ++stop;
22339969Sbostic }
22413070Ssam
22539969Sbostic /*
22639969Sbostic * quick list of primes <= pr_limit
22739969Sbostic */
22839969Sbostic if (start <= *pr_limit) {
22939969Sbostic /* skip primes up to the start value */
23039969Sbostic for (p = &prime[0], factor = prime[0];
23166292Sbostic factor < stop && p <= pr_limit; factor = *(++p)) {
23239969Sbostic if (factor >= start) {
23339969Sbostic printf("%u\n", factor);
23439969Sbostic }
23539969Sbostic }
23639969Sbostic /* return early if we are done */
23739969Sbostic if (p <= pr_limit) {
23839969Sbostic return;
23939969Sbostic }
24039969Sbostic start = *pr_limit+2;
24139969Sbostic }
24213070Ssam
24339969Sbostic /*
24439969Sbostic * we shall sieve a bytemap window, note primes and move the window
24539969Sbostic * upward until we pass the stop point
24639969Sbostic */
24739969Sbostic while (start < stop) {
24839969Sbostic /*
24939969Sbostic * factor out 3, 5, 7, 11 and 13
25039969Sbostic */
25139969Sbostic /* initial pattern copy */
25239969Sbostic factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */
25339969Sbostic memcpy(table, &pattern[factor], pattern_size-factor);
25439969Sbostic /* main block pattern copies */
25539969Sbostic for (fact_lim=pattern_size-factor;
25666292Sbostic fact_lim+pattern_size<=TABSIZE; fact_lim+=pattern_size) {
25739969Sbostic memcpy(&table[fact_lim], pattern, pattern_size);
25839969Sbostic }
25939969Sbostic /* final block pattern copy */
26039969Sbostic memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim);
26139969Sbostic
26239969Sbostic /*
26339969Sbostic * sieve for primes 17 and higher
26439969Sbostic */
26539969Sbostic /* note highest useful factor and sieve spot */
26639969Sbostic if (stop-start > TABSIZE+TABSIZE) {
26739969Sbostic tab_lim = &table[TABSIZE]; /* sieve it all */
26839969Sbostic fact_lim = (int)sqrt(
26939969Sbostic (double)(start)+TABSIZE+TABSIZE+1.0);
27039969Sbostic } else {
27139969Sbostic tab_lim = &table[(stop-start)/2]; /* partial sieve */
27239969Sbostic fact_lim = (int)sqrt((double)(stop)+1.0);
27339969Sbostic }
27439969Sbostic /* sieve for factors >= 17 */
27539969Sbostic factor = 17; /* 17 is first prime to use */
27639969Sbostic p = &prime[7]; /* 19 is next prime, pi(19)=7 */
27739969Sbostic do {
27839969Sbostic /* determine the factor's initial sieve point */
27939969Sbostic q = (char *)(start%factor); /* temp storage for mod */
28039969Sbostic if ((int)q & 0x1) {
28139969Sbostic q = &table[(factor-(int)q)/2];
28239969Sbostic } else {
28339969Sbostic q = &table[q ? factor-((int)q/2) : 0];
28439969Sbostic }
28539969Sbostic /* sive for our current factor */
28639969Sbostic for ( ; q < tab_lim; q += factor) {
28739969Sbostic *q = '\0'; /* sieve out a spot */
28839969Sbostic }
28939969Sbostic } while ((factor=(ubig)(*(p++))) <= fact_lim);
29039969Sbostic
29139969Sbostic /*
29239969Sbostic * print generated primes
29339969Sbostic */
29439969Sbostic for (q = table; q < tab_lim; ++q, start+=2) {
29539969Sbostic if (*q) {
29639969Sbostic printf("%u\n", start);
29739969Sbostic }
29839969Sbostic }
29913070Ssam }
30013070Ssam }
30166292Sbostic
30266292Sbostic void
usage()30366292Sbostic usage()
30466292Sbostic {
30566410Sbostic (void)fprintf(stderr, "usage: primes [start [stop]]\n");
30666292Sbostic exit(1);
30766292Sbostic }
308