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. 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1989, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95"; 42 #endif 43 static const char rcsid[] = 44 "$FreeBSD$"; 45 #endif /* not lint */ 46 47 /* 48 * primes - generate a table of primes between two values 49 * 50 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo 51 * 52 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\ 53 * 54 * usage: 55 * primes [-h] [start [stop]] 56 * 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. 60 * 61 * validation check: there are 664579 primes between 0 and 10^7 62 */ 63 64 #include <ctype.h> 65 #include <err.h> 66 #include <errno.h> 67 #include <inttypes.h> 68 #include <limits.h> 69 #include <math.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 75 #include "primes.h" 76 77 /* 78 * Eratosthenes sieve table 79 * 80 * We only sieve the odd numbers. The base of our sieve windows are always 81 * odd. If the base of table is 1, table[i] represents 2*i-1. After the 82 * sieve, table[i] == 1 if and only if 2*i-1 is prime. 83 * 84 * We make TABSIZE large to reduce the overhead of inner loop setup. 85 */ 86 static char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */ 87 88 static int hflag; 89 90 static void primes(ubig, ubig); 91 static ubig read_num_buf(void); 92 static void usage(void); 93 94 int 95 main(int argc, char *argv[]) 96 { 97 ubig start; /* where to start generating */ 98 ubig stop; /* don't generate at or above this value */ 99 int ch; 100 char *p; 101 102 while ((ch = getopt(argc, argv, "h")) != -1) 103 switch (ch) { 104 case 'h': 105 hflag++; 106 break; 107 case '?': 108 default: 109 usage(); 110 } 111 argc -= optind; 112 argv += optind; 113 114 start = 0; 115 stop = SPSPMAX; 116 117 /* 118 * Convert low and high args. Strtoumax(3) sets errno to 119 * ERANGE if the number is too large, but, if there's 120 * a leading minus sign it returns the negation of the 121 * result of the conversion, which we'd rather disallow. 122 */ 123 switch (argc) { 124 case 2: 125 /* Start and stop supplied on the command line. */ 126 if (argv[0][0] == '-' || argv[1][0] == '-') 127 errx(1, "negative numbers aren't permitted."); 128 129 errno = 0; 130 start = strtoumax(argv[0], &p, 0); 131 if (errno) 132 err(1, "%s", argv[0]); 133 if (*p != '\0') 134 errx(1, "%s: illegal numeric format.", argv[0]); 135 136 errno = 0; 137 stop = strtoumax(argv[1], &p, 0); 138 if (errno) 139 err(1, "%s", argv[1]); 140 if (*p != '\0') 141 errx(1, "%s: illegal numeric format.", argv[1]); 142 if (stop > SPSPMAX) 143 errx(1, "%s: stop value too large.", argv[1]); 144 break; 145 case 1: 146 /* Start on the command line. */ 147 if (argv[0][0] == '-') 148 errx(1, "negative numbers aren't permitted."); 149 150 errno = 0; 151 start = strtoumax(argv[0], &p, 0); 152 if (errno) 153 err(1, "%s", argv[0]); 154 if (*p != '\0') 155 errx(1, "%s: illegal numeric format.", argv[0]); 156 break; 157 case 0: 158 start = read_num_buf(); 159 break; 160 default: 161 usage(); 162 } 163 164 if (start > stop) 165 errx(1, "start value must be less than stop value."); 166 primes(start, stop); 167 return (0); 168 } 169 170 /* 171 * read_num_buf -- 172 * This routine returns a number n, where 0 <= n && n <= BIG. 173 */ 174 static ubig 175 read_num_buf(void) 176 { 177 ubig val; 178 char *p, buf[LINE_MAX]; /* > max number of digits. */ 179 180 for (;;) { 181 if (fgets(buf, sizeof(buf), stdin) == NULL) { 182 if (ferror(stdin)) 183 err(1, "stdin"); 184 exit(0); 185 } 186 for (p = buf; isblank(*p); ++p); 187 if (*p == '\n' || *p == '\0') 188 continue; 189 if (*p == '-') 190 errx(1, "negative numbers aren't permitted."); 191 errno = 0; 192 val = strtoumax(buf, &p, 0); 193 if (errno) 194 err(1, "%s", buf); 195 if (*p != '\n') 196 errx(1, "%s: illegal numeric format.", buf); 197 return (val); 198 } 199 } 200 201 /* 202 * primes - sieve and print primes from start up to and but not including stop 203 */ 204 static void 205 primes(ubig start, ubig stop) 206 { 207 char *q; /* sieve spot */ 208 ubig factor; /* index and factor */ 209 char *tab_lim; /* the limit to sieve on the table */ 210 const ubig *p; /* prime table pointer */ 211 ubig fact_lim; /* highest prime for current block */ 212 ubig mod; /* temp storage for mod */ 213 214 /* 215 * A number of systems can not convert double values into unsigned 216 * longs when the values are larger than the largest signed value. 217 * We don't have this problem, so we can go all the way to BIG. 218 */ 219 if (start < 3) { 220 start = (ubig)2; 221 } 222 if (stop < 3) { 223 stop = (ubig)2; 224 } 225 if (stop <= start) { 226 return; 227 } 228 229 /* 230 * be sure that the values are odd, or 2 231 */ 232 if (start != 2 && (start&0x1) == 0) { 233 ++start; 234 } 235 if (stop != 2 && (stop&0x1) == 0) { 236 ++stop; 237 } 238 239 /* 240 * quick list of primes <= pr_limit 241 */ 242 if (start <= *pr_limit) { 243 /* skip primes up to the start value */ 244 for (p = &prime[0], factor = prime[0]; 245 factor < stop && p <= pr_limit; factor = *(++p)) { 246 if (factor >= start) { 247 printf(hflag ? "%" PRIx64 "\n" : "%" PRIu64 "\n", factor); 248 } 249 } 250 /* return early if we are done */ 251 if (p <= pr_limit) { 252 return; 253 } 254 start = *pr_limit+2; 255 } 256 257 /* 258 * we shall sieve a bytemap window, note primes and move the window 259 * upward until we pass the stop point 260 */ 261 while (start < stop) { 262 /* 263 * factor out 3, 5, 7, 11 and 13 264 */ 265 /* initial pattern copy */ 266 factor = (start%(2*3*5*7*11*13))/2; /* starting copy spot */ 267 memcpy(table, &pattern[factor], pattern_size-factor); 268 /* main block pattern copies */ 269 for (fact_lim=pattern_size-factor; 270 fact_lim+pattern_size<=TABSIZE; fact_lim+=pattern_size) { 271 memcpy(&table[fact_lim], pattern, pattern_size); 272 } 273 /* final block pattern copy */ 274 memcpy(&table[fact_lim], pattern, TABSIZE-fact_lim); 275 276 /* 277 * sieve for primes 17 and higher 278 */ 279 /* note highest useful factor and sieve spot */ 280 if (stop-start > TABSIZE+TABSIZE) { 281 tab_lim = &table[TABSIZE]; /* sieve it all */ 282 fact_lim = sqrt(start+1.0+TABSIZE+TABSIZE); 283 } else { 284 tab_lim = &table[(stop-start)/2]; /* partial sieve */ 285 fact_lim = sqrt(stop+1.0); 286 } 287 /* sieve for factors >= 17 */ 288 factor = 17; /* 17 is first prime to use */ 289 p = &prime[7]; /* 19 is next prime, pi(19)=7 */ 290 do { 291 /* determine the factor's initial sieve point */ 292 mod = start%factor; 293 if (mod & 0x1) { 294 q = &table[(factor-mod)/2]; 295 } else { 296 q = &table[mod ? factor-(mod/2) : 0]; 297 } 298 /* sive for our current factor */ 299 for ( ; q < tab_lim; q += factor) { 300 *q = '\0'; /* sieve out a spot */ 301 } 302 factor = *p++; 303 } while (factor <= fact_lim); 304 305 /* 306 * print generated primes 307 */ 308 for (q = table; q < tab_lim; ++q, start+=2) { 309 if (*q) { 310 if (start > SIEVEMAX) { 311 if (!isprime(start)) 312 continue; 313 } 314 printf(hflag ? "%" PRIx64 "\n" : "%" PRIu64 "\n", start); 315 } 316 } 317 } 318 } 319 320 static void 321 usage(void) 322 { 323 fprintf(stderr, "usage: primes [-h] [start [stop]]\n"); 324 exit(1); 325 } 326