modified: diffout.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / help / ptest
blob218f3fe5529e1dbdfa49767b344447d496aabbf4
1 NAME
2     ptest - probabilistic test of primality
4 SYNOPSIS
5     ptest(n [,count [,skip]])
7 TYPES
8     n           integer
9     count       integer with absolute value less than 2^24, defaults to 1
10     skip        integer, defaults to 1
12     return      0 or 1
14 DESCRIPTION
15     In ptest(n, ...) the sign of n is ignored; here we assume n >= 0.
17     ptest(n, count, skip) always returns 1 if n is prime; equivalently,
18     if 0 is returned then n is not prime.
20     If n is even, 1 is returned only if n = 2.
22     If count >= 0 and n < 2^32, ptest(n,...) essentially calls isprime(n)
23     and returns 1 only if n is prime.
25     If count >= 0, n > 2^32, and n is divisible by a prime <= 101, then
26     ptest(n,...) returns 0.
28     If count is zero, and none of the above cases have resulted in 0 being
29     returned, 1 is returned.
31     In other cases (which includes all cases with count < 0), tests are
32     made for abs(count) bases b:  if n - 1 = 2^s * m where m is odd,
33     the test for base b of possible primality is passed if b is a
34     multiple of n or b^m = 1 (mod n) or b^(2^j * m) = n - 1 (mod n) for
35     some j where 0 <= j < s; integers that pass the test are called
36     strong probable primes for the base b; composite integers that pass
37     the test are called strong pseudoprimes for the base b; Since
38     the test for base b depends on b % n, and bases 0, 1 and n - 1 are
39     trivial (n is always a strong probable prime for these bases), it
40     is sufficient to consider 1 < b < n - 1.
42     The bases for ptest(n, count, skip) are selected as follows:
44         skip = 0:       random in [2, n-2]
45         skip = 1:       successive primes 2, 3, 5, ...
46                         not exceeding min(n, 65536)
47         otherwise:      successive integers skip, skip + 1, ...,
48                         skip+abs(count)-1
50     In particular, if m > 0, ptest(n, -m, 2) == 1 shows that n is either
51     prime or a strong pseudoprime for all positive integer bases <= m + 1.
52     If 1 < b < n - 1, ptest(n, -1, b) == 1 if and only if n is
53     a strong pseudoprime for the base b.
55     For the random case (skip = 0), the probability that any one test
56     with random base b will return 1 if n is composite is always
57     less than 1/4, so with count = k, the probability is less
58     than 1/4^k.  For most values of n the probability is much
59     smaller, possible zero.
61 RUNTIME
62     If n is composite, ptest(n, 1, skip) is usually faster than
63     ptest(n, -1, skip), much faster if n is divisible by a small
64     prime.  If n is prime, ptest(n, -1, skip) is usually faster than
65     ptest(n, 1, skip), possibly much faster if n < 2^32, only slightly
66     faster if n > 2^32.
68     If n is a large prime (say 50 or more decimal digits), the runtime
69     for ptest(n, count, skip) will usually be roughly K * abs(count) *
70     ln(n)^3 for some constant K.  For composite n with
71     highbit(n) = N, the compositeness is detected quickly if n is
72     divisible by a small prime and count >= 0; otherwise, if count is
73     not zero, usually only one test is required to establish
74     compositeness, so the runtime will probably be about K * N^3.   For
75     some rare values of composite n, high values of count may be
76     required to establish the compositeness.
78     If the word-count for n is less than conf("redc2"), REDC algorithms
79     are used in evaluating ptest(n, count, skip) when small-factor
80     cases have been eliminated.  For small word-counts (say < 10)
81     this may more than double the speed of evaluation compared with
82     the standard algorithms.
84 EXAMPLE
85     ; print ptest(103^3 * 3931, 0), ptest(4294967291,0)
86     1 1
88     In the first example, the first argument > 2^32; in the second the
89     first argument is the largest prime less than 2^32.
91     ; print ptest(121,-1,2), ptest(121,-1,3), ptest(121,-2,2)
92     0 1 0
94     121 is the smallest strong pseudoprime to the base 3.
96     ; x = 151 * 751 * 28351
97     ; print x, ptest(x,-4,1), ptest(x,-5,1)
98     3215031751 1 0
100     The integer x in this example is the smallest positive integer that is
101     a strong pseudoprime to each of the first four primes 2, 3, 5, 7, but
102     not to base 11.  The probability that ptest(x,-1,0) will return 1 is
103     about .23.
105     ; for (i = 0; i < 11; i++) print ptest(91,-1,0),:; print;
106     0 0 0 1 0 0 0 0 0 0 1
108     The results for this example depend on the state of the
109     random number generator; the expectation is that 1 will occur twice.
111     ; a = 24444516448431392447461 * 48889032896862784894921;
112     ; print ptest(a,11,1), ptest(a,12,1), ptest(a,20,2), ptest(a,21,2)
113     1 0 1 0
115     These results show that a is a strong pseudoprime for all 11 prime
116     bases less than or equal to 31, and for all positive integer bases
117     less than or equal to 21, but not for the bases 37 and 22.  The
118     probability that ptest(a,-1,0) (or ptest(a,1,0)) will return 1 is
119     about 0.19.
121 LIMITS
122     none
124 LINK LIBRARY
125     BOOL qprimetest(NUMBER *n, NUMBER *count, NUMBER *skip)
126     BOOL zprimetest(ZVALUE n, long count, long skip)
128 SEE ALSO
129     factor, isprime, lfactor, nextcand, nextprime, prevcand, prevprime,
130     pfact, pix
132 ## Copyright (C) 1999-2006  Landon Curt Noll
134 ## Calc is open software; you can redistribute it and/or modify it under
135 ## the terms of the version 2.1 of the GNU Lesser General Public License
136 ## as published by the Free Software Foundation.
138 ## Calc is distributed in the hope that it will be useful, but WITHOUT
139 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
140 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
141 ## Public License for more details.
143 ## A copy of version 2.1 of the GNU Lesser General Public License is
144 ## distributed with calc under the filename COPYING-LGPL.  You should have
145 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
146 ## 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
148 ## @(#) $Revision: 30.2 $
149 ## @(#) $Id: ptest,v 30.2 2007/09/01 19:53:15 chongo Exp $
150 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/ptest,v $
152 ## Under source code control:   1996/02/25 00:27:43
153 ## File existed as early as:    1996
155 ## chongo <was here> /\oo/\     http://www.isthe.com/chongo/
156 ## Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/