-/* $NetBSD: primes.c,v 1.5 1995/04/24 12:24:47 cgd Exp $ */
+/* $NetBSD: primes.c,v 1.11 2003/08/07 09:37:33 agc Exp $ */
/*
* Copyright (c) 1989, 1993
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1989, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
-static char sccsid[] = "@(#)primes.c 8.4 (Berkeley) 3/21/94";
+static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
#else
-static char rcsid[] = "$NetBSD: primes.c,v 1.5 1995/04/24 12:24:47 cgd Exp $";
+__RCSID("$NetBSD: primes.c,v 1.11 2003/08/07 09:37:33 agc Exp $");
#endif
#endif /* not lint */
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include "primes.h"
* We are able to sieve 2^32-1 because this byte table yields all primes
* up to 65537 and 65537^2 > 2^32-1.
*/
-extern ubig prime[];
-extern ubig *pr_limit; /* largest prime in the prime array */
+extern const ubig prime[];
+extern const ubig *pr_limit; /* largest prime in the prime array */
/*
* To avoid excessive sieves for small factors, we use the table below to
* setup our sieve blocks. Each element represents a odd number starting
* with 1. All non-zero elements are factors of 3, 5, 7, 11 and 13.
*/
-extern char pattern[];
-extern int pattern_size; /* length of pattern array */
+extern const char pattern[];
+extern const int pattern_size; /* length of pattern array */
+int main __P((int, char *[]));
void primes __P((ubig, ubig));
ubig read_num_buf __P((void));
-void usage __P((void));
+void usage __P((void)) __attribute__((__noreturn__));
int
main(argc, argv)
int ch;
char *p;
- while ((ch = getopt(argc, argv, "")) != EOF)
+ while ((ch = getopt(argc, argv, "")) != -1)
switch (ch) {
case '?':
default:
ubig start; /* where to start generating */
ubig stop; /* don't generate at or above this value */
{
- register char *q; /* sieve spot */
- register ubig factor; /* index and factor */
- register char *tab_lim; /* the limit to sieve on the table */
- register ubig *p; /* prime table pointer */
- register ubig fact_lim; /* highest prime for current block */
+ char *q; /* sieve spot */
+ ubig factor; /* index and factor */
+ char *tab_lim; /* the limit to sieve on the table */
+ const ubig *p; /* prime table pointer */
+ ubig fact_lim; /* highest prime for current block */
+ ubig mod; /* temp storage for mod */
/*
* A number of systems can not convert double values into unsigned
for (p = &prime[0], factor = prime[0];
factor < stop && p <= pr_limit; factor = *(++p)) {
if (factor >= start) {
- printf("%u\n", factor);
+ printf("%lu\n", (unsigned long) factor);
}
}
/* return early if we are done */
p = &prime[7]; /* 19 is next prime, pi(19)=7 */
do {
/* determine the factor's initial sieve point */
- q = (char *)(start%factor); /* temp storage for mod */
- if ((long)q & 0x1) {
- q = &table[(factor-(long)q)/2];
+ mod = start%factor;
+ if (mod & 0x1) {
+ q = &table[(factor-mod)/2];
} else {
- q = &table[q ? factor-((long)q/2) : 0];
+ q = &table[mod ? factor-(mod/2) : 0];
}
/* sive for our current factor */
for ( ; q < tab_lim; q += factor) {
*/
for (q = table; q < tab_lim; ++q, start+=2) {
if (*q) {
- printf("%u\n", start);
+ printf("%lu\n", (unsigned long) start);
}
}
}