Imported and adapted from FreeBSD svn r272166 and r272207; this fixes
false positives for products of primes larger than 2^16. For example, before this commit: $ /usr/games/primes 4295360521 4295360522 4295360521 but $ /usr/games/factor 4295360521 4295360521: 65539 65539 or $ /usr/games/primes 3825123056546413049 3825123056546413050 3825123056546413049 yet $ /usr/games/factor 3825123056546413049 3825123056546413049: 165479 23115459100831 or $ /usr/games/primes 18446744073709551577 18446744073709551577 although $ /usr/games/factor 18446744073709551577 18446744073709551577: 139646831 132095686967 Incidentally, the above examples show the smallest and largest cases that were erroneously stated as prime in the range 2^32 .. 3825123056546413049 .. 2^64; the primes(6) program now stops at 3825123056546413050 as primality tests on larger integers would be by brute force factorization. In addition, special to the NetBSD version: . for -d option, skip first difference when start is >65537 as it is incorrect . corrected usage to mention both the existing -d as well as the new -h option For original FreeBSD commit message by Colin Percival, see: http://svnweb.freebsd.org/base?view=revision&revision=272166
This commit is contained in:
parent
4460f134df
commit
bfe1fbfe02
@ -1,4 +1,4 @@
|
||||
.\" $NetBSD: factor.6,v 1.12 2010/05/15 21:22:39 joerg Exp $
|
||||
.\" $NetBSD: factor.6,v 1.13 2014/10/02 21:36:37 ast Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1989, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
@ -33,9 +33,7 @@
|
||||
.\" @(#)factor.6 8.1 (Berkeley) 5/31/93
|
||||
.\"
|
||||
.\"
|
||||
.\" By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
.\"
|
||||
.\" chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
.\" By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
.\"
|
||||
.Dd May 15, 2010
|
||||
.Dt FACTOR 6
|
||||
@ -88,10 +86,7 @@ is compiled without OpenSSL it is limited to the maximum value of
|
||||
.Vt unsigned long .
|
||||
.Sh DIAGNOSTICS
|
||||
Out of range or invalid input results in
|
||||
an appropriate error message
|
||||
being written to standard error.
|
||||
.\".Sh BUGS
|
||||
.\".Nm
|
||||
.\"cannot handle the
|
||||
.\".Dq 10 most wanted
|
||||
.\"factor list.
|
||||
an appropriate error message to standard error.
|
||||
.Sh AUTHORS
|
||||
Originally by
|
||||
.An Landon Curt Noll .
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: factor.c,v 1.26 2011/11/09 20:17:44 drochner Exp $ */
|
||||
/* $NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -42,16 +42,14 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)factor.c 8.4 (Berkeley) 5/4/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: factor.c,v 1.26 2011/11/09 20:17:44 drochner Exp $");
|
||||
__RCSID("$NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
/*
|
||||
* factor - factor a number into primes
|
||||
*
|
||||
* By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
*
|
||||
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
* By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
*
|
||||
* usage:
|
||||
* factor [number] ...
|
||||
@ -72,6 +70,7 @@ __RCSID("$NetBSD: factor.c,v 1.26 2011/11/09 20:17:44 drochner Exp $");
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
#include <openssl/bn.h>
|
||||
@ -93,8 +92,7 @@ static int BN_dec2bn(BIGNUM **a, const char *str);
|
||||
* 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 const ubig prime[];
|
||||
extern const ubig *pr_limit; /* largest prime in the prime array */
|
||||
|
||||
#if 0 /* debugging: limit table use to stress the "pollard" code */
|
||||
#define pr_limit &prime[0]
|
||||
#endif
|
||||
@ -198,7 +196,7 @@ main(int argc, char *argv[])
|
||||
static void
|
||||
pr_fact(BIGNUM *val)
|
||||
{
|
||||
const ubig *fact; /* The factor found. */
|
||||
const uint64_t *fact; /* The factor found. */
|
||||
|
||||
/* Firewall - catch 0 and 1. */
|
||||
if (BN_is_zero(val) || BN_is_one(val))
|
||||
@ -239,7 +237,7 @@ pr_fact(BIGNUM *val)
|
||||
|
||||
/* Divide factor out until none are left. */
|
||||
do {
|
||||
printf(" %lu", *fact);
|
||||
printf(" %" PRIu64, *fact);
|
||||
BN_div_word(val, (BN_ULONG)*fact);
|
||||
} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
# $NetBSD: Makefile,v 1.7 2004/02/08 13:16:25 jsm Exp $
|
||||
# $NetBSD: Makefile,v 1.8 2014/10/02 21:36:37 ast Exp $
|
||||
# @(#)Makefile 8.1 (Berkeley) 5/31/93
|
||||
|
||||
PROG= primes
|
||||
SRCS= pattern.c pr_tbl.c primes.c
|
||||
SRCS= pattern.c pr_tbl.c primes.c spsp.c
|
||||
MAN= primes.6
|
||||
DPADD= ${LIBM}
|
||||
LDADD= -lm
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: pattern.c,v 1.6 2003/08/07 09:37:33 agc Exp $ */
|
||||
/* $NetBSD: pattern.c,v 1.7 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -32,21 +32,20 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef lint
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)pattern.c 8.1 (Berkeley) 5/31/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: pattern.c,v 1.6 2003/08/07 09:37:33 agc Exp $");
|
||||
__RCSID("$NetBSD: pattern.c,v 1.7 2014/10/02 21:36:37 ast Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
/*
|
||||
* pattern - the Eratosthenes sieve on odd numbers for 3,5,7,11 and 13
|
||||
*
|
||||
* By: Landon Curt Noll chongo@toad.com
|
||||
*
|
||||
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
* By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
*
|
||||
* To avoid excessive sieves for small factors, we use the table below to
|
||||
* setup our sieve blocks. Each element represents a odd number starting
|
||||
@ -440,4 +439,4 @@ const char pattern[] = {
|
||||
0,0,1,1,0,0,0,0,1,1,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,0,1,1,0,1,0,0,0,1,0,0,1,0,1,
|
||||
0,0,1,1,0,1,0,0,1,1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0,0,1
|
||||
};
|
||||
const int pattern_size = (sizeof(pattern)/sizeof(pattern[0]));
|
||||
const size_t pattern_size = (sizeof(pattern)/sizeof(pattern[0]));
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: pr_tbl.c,v 1.7 2003/08/07 09:37:33 agc Exp $ */
|
||||
/* $NetBSD: pr_tbl.c,v 1.8 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -37,24 +37,24 @@
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)pr_tbl.c 8.1 (Berkeley) 5/31/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: pr_tbl.c,v 1.7 2003/08/07 09:37:33 agc Exp $");
|
||||
__RCSID("$NetBSD: pr_tbl.c,v 1.8 2014/10/02 21:36:37 ast Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
/*
|
||||
* prime - prime table
|
||||
*
|
||||
* By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
* By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
*
|
||||
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
*
|
||||
* We are able to sieve 2^32-1 because this table has primes up to 65537
|
||||
* We are able to sieve 2^32-1 because this table has primes up to 65537
|
||||
* and 65537^2 > 2^32-1.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "primes.h"
|
||||
|
||||
const ubig prime[] = {
|
||||
const uint64_t prime[] = {
|
||||
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,
|
||||
107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,
|
||||
211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,
|
||||
@ -546,4 +546,4 @@ const ubig prime[] = {
|
||||
};
|
||||
|
||||
/* pr_limit - largest prime in the prime table */
|
||||
const ubig *pr_limit = &prime[(sizeof(prime)/sizeof(prime[0]))-1];
|
||||
const uint64_t *const pr_limit = &prime[(sizeof(prime)/sizeof(prime[0]))-1];
|
||||
|
@ -1,4 +1,4 @@
|
||||
.\" $NetBSD: primes.6,v 1.3 2008/02/03 03:29:17 wiz Exp $
|
||||
.\" $NetBSD: primes.6,v 1.4 2014/10/02 21:36:37 ast Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1989, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
@ -33,9 +33,7 @@
|
||||
.\" @(#)factor.6 8.1 (Berkeley) 5/31/93
|
||||
.\"
|
||||
.\"
|
||||
.\" By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
.\"
|
||||
.\" chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
.\" By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
.\"
|
||||
.Dd February 3, 2008
|
||||
.Dt PRIMES 6
|
||||
@ -46,6 +44,7 @@
|
||||
.Sh SYNOPSIS
|
||||
.Nm primes
|
||||
.Op Fl d
|
||||
.Op Fl h
|
||||
.Op Ar start Op Ar stop
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
@ -61,18 +60,18 @@ value must be at least 0 and not greater than
|
||||
.Ar stop .
|
||||
The
|
||||
.Ar stop
|
||||
value must not be greater than 4294967295.
|
||||
value must not be greater than 3825123056546413050.
|
||||
The default value of
|
||||
.Ar stop
|
||||
is 4294967295.
|
||||
is 3825123056546413050.
|
||||
.Pp
|
||||
When the
|
||||
.Nm
|
||||
utility is invoked with no arguments,
|
||||
.Ar start
|
||||
is read from standard input.
|
||||
is read from standard input and
|
||||
.Ar stop
|
||||
is taken to be 4294967295.
|
||||
is taken to be 3825123056546413050.
|
||||
The
|
||||
.Ar start
|
||||
value may be preceded by a single
|
||||
@ -81,15 +80,34 @@ The
|
||||
.Ar start
|
||||
value is terminated by a non-digit character (such as a newline).
|
||||
The input line must not be longer than 255 characters.
|
||||
.Pp
|
||||
When given the
|
||||
.Fl d
|
||||
argument,
|
||||
.Nm
|
||||
prints the difference between the current and the previous prime.
|
||||
.Pp
|
||||
When given the
|
||||
.Fl h
|
||||
argument,
|
||||
.Nm
|
||||
prints the prime numbers in hexadecimal.
|
||||
.Sh DIAGNOSTICS
|
||||
Out of range or invalid input results in
|
||||
an appropriate error message
|
||||
being written to standard error.
|
||||
.Sh BUGS
|
||||
an appropriate error message to standard error.
|
||||
.Sh AUTHORS
|
||||
Originally by
|
||||
.An Landon Curt Noll ,
|
||||
extended to some 64-bit primes by
|
||||
.An Colin Percival .
|
||||
.Sh CAVEATS
|
||||
This
|
||||
.Nm
|
||||
won't get you a world record.
|
||||
program won't get you a world record.
|
||||
.Pp
|
||||
The program is not able to list primes between
|
||||
3825123056546413050 and 18446744073709551615 (2^64
|
||||
- 1) as it relies on strong pseudoprime tests after
|
||||
sieving, and it is yet unknown how many of those
|
||||
tests are needed to prove primality for integers
|
||||
larger than 3825123056546413050.
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: primes.c,v 1.19 2011/08/30 02:58:04 jakllsch Exp $ */
|
||||
/* $NetBSD: primes.c,v 1.20 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -42,23 +42,23 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: primes.c,v 1.19 2011/08/30 02:58:04 jakllsch Exp $");
|
||||
__RCSID("$NetBSD: primes.c,v 1.20 2014/10/02 21:36:37 ast Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
/*
|
||||
* primes - generate a table of primes between two values
|
||||
*
|
||||
* By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
*
|
||||
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
* By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
*
|
||||
* usage:
|
||||
* primes [start [stop]]
|
||||
* primes [-d] [-h] [start [stop]]
|
||||
*
|
||||
* Print primes >= start and < stop. If stop is omitted,
|
||||
* the value 4294967295 (2^32-1) is assumed. If start is
|
||||
* the value SPSPMAX is assumed. If start is
|
||||
* omitted, start is read from standard input.
|
||||
* -h: print primes in hexadecimal
|
||||
* -d: print difference to previous prime, e.g. 3 (1)
|
||||
*
|
||||
* validation check: there are 664579 primes between 0 and 10^7
|
||||
*/
|
||||
@ -66,11 +66,12 @@ __RCSID("$NetBSD: primes.c,v 1.19 2011/08/30 02:58:04 jakllsch Exp $");
|
||||
#include <ctype.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "primes.h"
|
||||
@ -80,48 +81,35 @@ __RCSID("$NetBSD: primes.c,v 1.19 2011/08/30 02:58:04 jakllsch Exp $");
|
||||
*
|
||||
* We only sieve the odd numbers. The base of our sieve windows are always
|
||||
* odd. If the base of table is 1, table[i] represents 2*i-1. After the
|
||||
* sieve, table[i] == 1 if and only iff 2*i-1 is prime.
|
||||
* sieve, table[i] == 1 if and only if 2*i-1 is prime.
|
||||
*
|
||||
* We make TABSIZE large to reduce the overhead of inner loop setup.
|
||||
*/
|
||||
static char table[TABSIZE]; /* Eratosthenes sieve of odd numbers */
|
||||
|
||||
/*
|
||||
* prime[i] is the (i-1)th prime.
|
||||
*
|
||||
* 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 const ubig prime[];
|
||||
extern const ubig *pr_limit; /* largest prime in the prime array */
|
||||
static int dflag, hflag;
|
||||
|
||||
/*
|
||||
* 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 const char pattern[];
|
||||
extern const int pattern_size; /* length of pattern array */
|
||||
|
||||
static int dflag;
|
||||
|
||||
static void primes(ubig, ubig);
|
||||
static ubig read_num_buf(void);
|
||||
static void primes(uint64_t, uint64_t);
|
||||
static uint64_t read_num_buf(void);
|
||||
static void usage(void) __dead;
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
ubig start; /* where to start generating */
|
||||
ubig stop; /* don't generate at or above this value */
|
||||
uint64_t start; /* where to start generating */
|
||||
uint64_t stop; /* don't generate at or above this value */
|
||||
int ch;
|
||||
char *p;
|
||||
|
||||
while ((ch = getopt(argc, argv, "d")) != -1)
|
||||
while ((ch = getopt(argc, argv, "dh")) != -1)
|
||||
switch (ch) {
|
||||
case 'd':
|
||||
dflag++;
|
||||
break;
|
||||
case 'h':
|
||||
hflag++;
|
||||
break;
|
||||
case '?':
|
||||
default:
|
||||
usage();
|
||||
@ -130,10 +118,10 @@ main(int argc, char *argv[])
|
||||
argv += optind;
|
||||
|
||||
start = 0;
|
||||
stop = BIG;
|
||||
stop = SPSPMAX;
|
||||
|
||||
/*
|
||||
* Convert low and high args. Strtoul(3) sets errno to
|
||||
* Convert low and high args. Strtoumax(3) sets errno to
|
||||
* ERANGE if the number is too large, but, if there's
|
||||
* a leading minus sign it returns the negation of the
|
||||
* result of the conversion, which we'd rather disallow.
|
||||
@ -145,18 +133,21 @@ main(int argc, char *argv[])
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
|
||||
errno = 0;
|
||||
start = strtoul(argv[0], &p, 10);
|
||||
start = strtoumax(argv[0], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[0]);
|
||||
if (*p != '\0')
|
||||
errx(1, "%s: illegal numeric format.", argv[0]);
|
||||
|
||||
errno = 0;
|
||||
stop = strtoul(argv[1], &p, 10);
|
||||
stop = strtoumax(argv[1], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[1]);
|
||||
if (*p != '\0')
|
||||
errx(1, "%s: illegal numeric format.", argv[1]);
|
||||
if (stop > SPSPMAX)
|
||||
errx(1, "%s: stop value too large (>%" PRIu64 ").",
|
||||
argv[1], (uint64_t) SPSPMAX);
|
||||
break;
|
||||
case 1:
|
||||
/* Start on the command line. */
|
||||
@ -164,7 +155,7 @@ main(int argc, char *argv[])
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
|
||||
errno = 0;
|
||||
start = strtoul(argv[0], &p, 10);
|
||||
start = strtoumax(argv[0], &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", argv[0]);
|
||||
if (*p != '\0')
|
||||
@ -180,18 +171,18 @@ main(int argc, char *argv[])
|
||||
if (start > stop)
|
||||
errx(1, "start value must be less than stop value.");
|
||||
primes(start, stop);
|
||||
exit(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* read_num_buf --
|
||||
* This routine returns a number n, where 0 <= n && n <= BIG.
|
||||
* This routine returns a number n, where 0 <= n && n <= ULONG_MAX.
|
||||
*/
|
||||
ubig
|
||||
static uint64_t
|
||||
read_num_buf(void)
|
||||
{
|
||||
ubig val;
|
||||
char *p, buf[100]; /* > max number of digits. */
|
||||
uint64_t val;
|
||||
char *p, buf[LINE_MAX]; /* > max number of digits. */
|
||||
|
||||
for (;;) {
|
||||
if (fgets(buf, sizeof(buf), stdin) == NULL) {
|
||||
@ -205,7 +196,7 @@ read_num_buf(void)
|
||||
if (*p == '-')
|
||||
errx(1, "negative numbers aren't permitted.");
|
||||
errno = 0;
|
||||
val = strtoul(buf, &p, 10);
|
||||
val = strtoumax(buf, &p, 0);
|
||||
if (errno)
|
||||
err(1, "%s", buf);
|
||||
if (*p != '\n')
|
||||
@ -216,31 +207,28 @@ read_num_buf(void)
|
||||
|
||||
/*
|
||||
* primes - sieve and print primes from start up to and but not including stop
|
||||
*
|
||||
* start where to start generating
|
||||
* stop don't generate at or above this value
|
||||
*/
|
||||
void
|
||||
primes(ubig start, ubig stop)
|
||||
static void
|
||||
primes(uint64_t start, uint64_t stop)
|
||||
{
|
||||
char *q; /* sieve spot */
|
||||
ubig factor; /* index and factor */
|
||||
uint64_t 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 */
|
||||
ubig prev = 0;
|
||||
const uint64_t *p; /* prime table pointer */
|
||||
uint64_t fact_lim; /* highest prime for current block */
|
||||
uint64_t mod; /* temp storage for mod */
|
||||
uint64_t prev = 0;
|
||||
|
||||
/*
|
||||
* A number of systems can not convert double values into unsigned
|
||||
* longs when the values are larger than the largest signed value.
|
||||
* We don't have this problem, so we can go all the way to BIG.
|
||||
* We don't have this problem, so we can go all the way to ULONG_MAX.
|
||||
*/
|
||||
if (start < 3) {
|
||||
start = (ubig)2;
|
||||
start = 2;
|
||||
}
|
||||
if (stop < 3) {
|
||||
stop = (ubig)2;
|
||||
stop = 2;
|
||||
}
|
||||
if (stop <= start) {
|
||||
return;
|
||||
@ -264,10 +252,9 @@ primes(ubig start, ubig stop)
|
||||
for (p = &prime[0], factor = prime[0];
|
||||
factor < stop && p <= pr_limit; factor = *(++p)) {
|
||||
if (factor >= start) {
|
||||
printf("%lu", (unsigned long) factor);
|
||||
printf(hflag ? "%" PRIx64 : "%" PRIu64, factor);
|
||||
if (dflag) {
|
||||
printf(" (%lu)",
|
||||
(unsigned long) factor - prev);
|
||||
printf(" (%" PRIu64 ")", factor - prev);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
@ -305,10 +292,10 @@ primes(ubig start, ubig stop)
|
||||
/* note highest useful factor and sieve spot */
|
||||
if (stop-start > TABSIZE+TABSIZE) {
|
||||
tab_lim = &table[TABSIZE]; /* sieve it all */
|
||||
fact_lim = sqrt((double)(start)+TABSIZE+TABSIZE+1.0);
|
||||
fact_lim = sqrt(start+1.0+TABSIZE+TABSIZE);
|
||||
} else {
|
||||
tab_lim = &table[(stop-start)/2]; /* partial sieve */
|
||||
fact_lim = sqrt((double)(stop)+1.0);
|
||||
fact_lim = sqrt(stop+1.0);
|
||||
}
|
||||
/* sieve for factors >= 17 */
|
||||
factor = 17; /* 17 is first prime to use */
|
||||
@ -325,28 +312,32 @@ primes(ubig start, ubig stop)
|
||||
for ( ; q < tab_lim; q += factor) {
|
||||
*q = '\0'; /* sieve out a spot */
|
||||
}
|
||||
} while ((factor=(ubig)(*(p++))) <= fact_lim);
|
||||
factor = *p++;
|
||||
} while (factor <= fact_lim);
|
||||
|
||||
/*
|
||||
* print generated primes
|
||||
*/
|
||||
for (q = table; q < tab_lim; ++q, start+=2) {
|
||||
if (*q) {
|
||||
printf("%lu", (unsigned long) start);
|
||||
if (dflag) {
|
||||
printf(" (%lu)",
|
||||
(unsigned long) start - prev);
|
||||
prev = start;
|
||||
if (start > SIEVEMAX) {
|
||||
if (!isprime(start))
|
||||
continue;
|
||||
}
|
||||
printf(hflag ? "%" PRIx64 : "%" PRIu64, start);
|
||||
if (dflag && (prev || (start <= *pr_limit))) {
|
||||
printf(" (%" PRIu64 ")", start - prev);
|
||||
}
|
||||
putchar('\n');
|
||||
prev = start;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
(void)fprintf(stderr, "usage: primes [-d] [start [stop]]\n");
|
||||
(void)fprintf(stderr, "usage: primes [-d] [-h] [start [stop]]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: primes.h,v 1.5 2003/08/07 09:37:34 agc Exp $ */
|
||||
/* $NetBSD: primes.h,v 1.6 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1989, 1993
|
||||
@ -37,14 +37,38 @@
|
||||
/*
|
||||
* primes - generate a table of primes between two values
|
||||
*
|
||||
* By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
||||
* By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
|
||||
*
|
||||
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
||||
*/
|
||||
|
||||
/* ubig is the type that holds a large unsigned value */
|
||||
typedef unsigned long ubig; /* must be >=32 bit unsigned value */
|
||||
#define BIG ULONG_MAX /* largest value will sieve */
|
||||
#include <stdint.h>
|
||||
|
||||
/* bytes in sieve table (must be > 3*5*7*11) */
|
||||
#define TABSIZE 256*1024
|
||||
|
||||
/*
|
||||
* prime[i] is the (i-1)th prime.
|
||||
*
|
||||
* 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 const uint64_t prime[]; /* must be >=32 bit unsigned values */
|
||||
extern const uint64_t *const pr_limit; /* largest prime in the prime array */
|
||||
|
||||
/* Maximum size sieving alone can handle. */
|
||||
#define SIEVEMAX 4295098368ULL
|
||||
|
||||
/*
|
||||
* To avoid excessive sieves for small factors, we use the table below to
|
||||
* setup our sieve blocks. Each element represents an odd number starting
|
||||
* with 1. All non-zero elements are factors of 3, 5, 7, 11 and 13.
|
||||
*/
|
||||
extern const char pattern[];
|
||||
extern const size_t pattern_size; /* length of pattern array */
|
||||
|
||||
/* Test for primality using strong pseudoprime tests. */
|
||||
int isprime(uint64_t);
|
||||
|
||||
/* Maximum value which the SPSP code can handle. */
|
||||
#define SPSPMAX 3825123056546413050ULL
|
||||
|
195
games/primes/spsp.c
Normal file
195
games/primes/spsp.c
Normal file
@ -0,0 +1,195 @@
|
||||
/* $NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2014 Colin Percival
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef lint
|
||||
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\
|
||||
The Regents of the University of California. All rights reserved.");
|
||||
#endif /* not lint */
|
||||
|
||||
#ifndef lint
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)primes.c 8.5 (Berkeley) 5/10/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: spsp.c,v 1.1 2014/10/02 21:36:37 ast Exp $");
|
||||
#endif
|
||||
#endif /* not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "primes.h"
|
||||
|
||||
/* Return a * b % n, where 0 <= a, b < 2^63, 0 < n < 2^63. */
|
||||
static uint64_t
|
||||
mulmod(uint64_t a, uint64_t b, uint64_t n)
|
||||
{
|
||||
uint64_t x = 0;
|
||||
|
||||
while (b != 0) {
|
||||
if (b & 1)
|
||||
x = (x + a) % n;
|
||||
a = (a + a) % n;
|
||||
b >>= 1;
|
||||
}
|
||||
|
||||
return (x);
|
||||
}
|
||||
|
||||
/* Return a^r % n, where 0 <= a < 2^63, 0 < n < 2^63. */
|
||||
static uint64_t
|
||||
powmod(uint64_t a, uint64_t r, uint64_t n)
|
||||
{
|
||||
uint64_t x = 1;
|
||||
|
||||
while (r != 0) {
|
||||
if (r & 1)
|
||||
x = mulmod(a, x, n);
|
||||
a = mulmod(a, a, n);
|
||||
r >>= 1;
|
||||
}
|
||||
|
||||
return (x);
|
||||
}
|
||||
|
||||
/* Return non-zero if n is a strong pseudoprime to base p. */
|
||||
static int
|
||||
spsp(uint64_t n, uint64_t p)
|
||||
{
|
||||
uint64_t x;
|
||||
uint64_t r = n - 1;
|
||||
int k = 0;
|
||||
|
||||
/* Compute n - 1 = 2^k * r. */
|
||||
while ((r & 1) == 0) {
|
||||
k++;
|
||||
r >>= 1;
|
||||
}
|
||||
|
||||
/* Compute x = p^r mod n. If x = 1, n is a p-spsp. */
|
||||
x = powmod(p, r, n);
|
||||
if (x == 1)
|
||||
return (1);
|
||||
|
||||
/* Compute x^(2^i) for 0 <= i < n. If any are -1, n is a p-spsp. */
|
||||
while (k > 0) {
|
||||
if (x == n - 1)
|
||||
return (1);
|
||||
x = powmod(x, 2, n);
|
||||
k--;
|
||||
}
|
||||
|
||||
/* Not a p-spsp. */
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Test for primality using strong pseudoprime tests. */
|
||||
int
|
||||
isprime(uint64_t _n)
|
||||
{
|
||||
uint64_t n = _n;
|
||||
|
||||
/*
|
||||
* Values from:
|
||||
* C. Pomerance, J.L. Selfridge, and S.S. Wagstaff, Jr.,
|
||||
* The pseudoprimes to 25 * 10^9, Math. Comp. 35(151):1003-1026, 1980.
|
||||
*/
|
||||
|
||||
/* No SPSPs to base 2 less than 2047. */
|
||||
if (!spsp(n, 2))
|
||||
return (0);
|
||||
if (n < 2047ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3 less than 1373653. */
|
||||
if (!spsp(n, 3))
|
||||
return (0);
|
||||
if (n < 1373653ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3,5 less than 25326001. */
|
||||
if (!spsp(n, 5))
|
||||
return (0);
|
||||
if (n < 25326001ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3,5,7 less than 3215031751. */
|
||||
if (!spsp(n, 7))
|
||||
return (0);
|
||||
if (n < 3215031751ULL)
|
||||
return (1);
|
||||
|
||||
/*
|
||||
* Values from:
|
||||
* G. Jaeschke, On strong pseudoprimes to several bases,
|
||||
* Math. Comp. 61(204):915-926, 1993.
|
||||
*/
|
||||
|
||||
/* No SPSPs to bases 2,3,5,7,11 less than 2152302898747. */
|
||||
if (!spsp(n, 11))
|
||||
return (0);
|
||||
if (n < 2152302898747ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3,5,7,11,13 less than 3474749660383. */
|
||||
if (!spsp(n, 13))
|
||||
return (0);
|
||||
if (n < 3474749660383ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3,5,7,11,13,17 less than 341550071728321. */
|
||||
if (!spsp(n, 17))
|
||||
return (0);
|
||||
if (n < 341550071728321ULL)
|
||||
return (1);
|
||||
|
||||
/* No SPSPs to bases 2,3,5,7,11,13,17,19 less than 341550071728321. */
|
||||
if (!spsp(n, 19))
|
||||
return (0);
|
||||
if (n < 341550071728321ULL)
|
||||
return (1);
|
||||
|
||||
/*
|
||||
* Value from:
|
||||
* Y. Jiang and Y. Deng, Strong pseudoprimes to the first eight prime
|
||||
* bases, Math. Comp. 83(290):2915-2924, 2014.
|
||||
*/
|
||||
|
||||
/* No SPSPs to bases 2..23 less than 3825123056546413051. */
|
||||
if (!spsp(n, 23))
|
||||
return (0);
|
||||
if (n < 3825123056546413051)
|
||||
return (1);
|
||||
|
||||
/* We can't handle values larger than this. */
|
||||
assert(n <= SPSPMAX);
|
||||
|
||||
/* UNREACHABLE */
|
||||
return (0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user