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