]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - primes/primes.c
1 /* $NetBSD: primes.c,v 1.22 2018/02/03 15:40:29 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
45 __RCSID("$NetBSD: primes.c,v 1.22 2018/02/03 15:40:29 christos Exp $");
50 * primes - generate a table of primes between two values
52 * By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
55 * primes [-dh] [start [stop]]
57 * Print primes >= start and < stop. If stop is omitted,
58 * the value SPSPMAX is assumed. If start is
59 * omitted, start is read from standard input.
60 * -d: print difference to previous prime, e.g. 3 (1)
61 * -h: print primes in hexadecimal
63 * validation check: there are 664579 primes between 0 and 10^7
80 * Eratosthenes sieve table
82 * We only sieve the odd numbers. The base of our sieve windows are always
83 * odd. If the base of table is 1, table[i] represents 2*i-1. After the
84 * sieve, table[i] == 1 if and only if 2*i-1 is prime.
86 * We make TABSIZE large to reduce the overhead of inner loop setup.
88 static char table
[TABSIZE
]; /* Eratosthenes sieve of odd numbers */
90 static int dflag
, hflag
;
92 static void primes(uint64_t, uint64_t);
93 static uint64_t read_num_buf(void);
94 static void usage(void) __dead
;
98 main(int argc
, char *argv
[])
100 uint64_t start
; /* where to start generating */
101 uint64_t stop
; /* don't generate at or above this value */
105 while ((ch
= getopt(argc
, argv
, "dh")) != -1)
121 stop
= (uint64_t)(-1);
124 * Convert low and high args. Strtoumax(3) sets errno to
125 * ERANGE if the number is too large, but, if there's
126 * a leading minus sign it returns the negation of the
127 * result of the conversion, which we'd rather disallow.
131 /* Start and stop supplied on the command line. */
132 if (argv
[0][0] == '-' || argv
[1][0] == '-')
133 errx(1, "negative numbers aren't permitted.");
136 start
= strtoumax(argv
[0], &p
, 0);
138 err(1, "%s", argv
[0]);
140 errx(1, "%s: illegal numeric format.", argv
[0]);
143 stop
= strtoumax(argv
[1], &p
, 0);
145 err(1, "%s", argv
[1]);
147 errx(1, "%s: illegal numeric format.", argv
[1]);
150 /* Start on the command line. */
151 if (argv
[0][0] == '-')
152 errx(1, "negative numbers aren't permitted.");
155 start
= strtoumax(argv
[0], &p
, 0);
157 err(1, "%s", argv
[0]);
159 errx(1, "%s: illegal numeric format.", argv
[0]);
162 start
= read_num_buf();
169 errx(1, "start value must be less than stop value.");
176 * This routine returns a number n, where 0 <= n && n <= ULONG_MAX.
182 char *p
, buf
[LINE_MAX
]; /* > max number of digits. */
185 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
190 for (p
= buf
; isblank((unsigned char)*p
); ++p
);
191 if (*p
== '\n' || *p
== '\0')
194 errx(1, "negative numbers aren't permitted.");
196 val
= strtoumax(buf
, &p
, 0);
200 errx(1, "%s: illegal numeric format.", buf
);
206 * primes - sieve and print primes from start up to and but not including stop
209 primes(uint64_t start
, uint64_t stop
)
211 char *q
; /* sieve spot */
212 uint64_t factor
; /* index and factor */
213 char *tab_lim
; /* the limit to sieve on the table */
214 const uint64_t *p
; /* prime table pointer */
215 uint64_t fact_lim
; /* highest prime for current block */
216 uint64_t mod
; /* temp storage for mod */
220 * A number of systems can not convert double values into unsigned
221 * longs when the values are larger than the largest signed value.
222 * We don't have this problem, so we can go all the way to ULONG_MAX.
235 * be sure that the values are odd, or 2
237 if (start
!= 2 && (start
&0x1) == 0) {
240 if (stop
!= 2 && (stop
&0x1) == 0) {
245 * quick list of primes <= pr_limit
247 if (start
<= *pr_limit
) {
248 /* skip primes up to the start value */
249 for (p
= &prime
[0], factor
= prime
[0];
250 factor
< stop
&& p
<= pr_limit
; factor
= *(++p
)) {
251 if (factor
>= start
) {
252 printf(hflag
? "%" PRIx64
: "%" PRIu64
, factor
);
254 printf(" (%" PRIu64
")", factor
- prev
);
260 /* return early if we are done */
268 * we shall sieve a bytemap window, note primes and move the window
269 * upward until we pass the stop point
271 while (start
< stop
) {
273 * factor out 3, 5, 7, 11 and 13
275 /* initial pattern copy */
276 factor
= (start
%(2*3*5*7*11*13))/2; /* starting copy spot */
277 memcpy(table
, &pattern
[factor
], pattern_size
-factor
);
278 /* main block pattern copies */
279 for (fact_lim
=pattern_size
-factor
;
280 fact_lim
+pattern_size
<=TABSIZE
; fact_lim
+=pattern_size
) {
281 memcpy(&table
[fact_lim
], pattern
, pattern_size
);
283 /* final block pattern copy */
284 memcpy(&table
[fact_lim
], pattern
, TABSIZE
-fact_lim
);
287 * sieve for primes 17 and higher
289 /* note highest useful factor and sieve spot */
290 if (stop
-start
> TABSIZE
+TABSIZE
) {
291 tab_lim
= &table
[TABSIZE
]; /* sieve it all */
292 fact_lim
= sqrt(start
+1.0+TABSIZE
+TABSIZE
);
294 tab_lim
= &table
[(stop
-start
)/2]; /* partial sieve */
295 fact_lim
= sqrt(stop
+1.0);
297 /* sieve for factors >= 17 */
298 factor
= 17; /* 17 is first prime to use */
299 p
= &prime
[7]; /* 19 is next prime, pi(19)=7 */
301 /* determine the factor's initial sieve point */
304 q
= &table
[(factor
-mod
)/2];
306 q
= &table
[mod
? factor
-(mod
/2) : 0];
308 /* sieve for our current factor */
309 for ( ; q
< tab_lim
; q
+= factor
) {
310 *q
= '\0'; /* sieve out a spot */
313 } while (factor
<= fact_lim
);
316 * print generated primes
318 for (q
= table
; q
< tab_lim
; ++q
, start
+=2) {
320 if (start
> SIEVEMAX
) {
324 printf(hflag
? "%" PRIx64
: "%" PRIu64
, start
);
325 if (dflag
&& (prev
|| (start
<= *pr_limit
))) {
326 printf(" (%" PRIu64
")", start
- prev
);
338 (void)fprintf(stderr
, "usage: primes [-dh] [start [stop]]\n");