NAME
    prevcand - previous candidate for primeness

SYNOPSIS
    prevcand(n [,count [, skip [, residue [, modulus]]]])

TYPES
    n		integer
    count	integer with absolute value less than 2^24, defaults to 1
    skip	integer, defaults to 1
    residue	integer, defaults to 0
    modulus	integer, defaults to 1

    return	integer

DESCRIPTION
    The sign of n is ignored; in the following it is assumed that n >= 0.

    prevcand(n, count, skip, residue, modulus) returns the greatest
    positive integer i less than abs(n) expressible as
    residue + k * modulus, where k is an integer, for which
    ptest(i,count,skip) == 1, or if there is no such integer i, zero.

    If n < 2^32, count >= 0, and the returned value i is not zero, i is
    definitely prime.  If n > 2^32, count != 0, and i is not zero,
    i is probably prime, particularly if abs(count) is greater than 1.

    With the default argument values, if n > 2, prevcand(n) returns the a
    probably prime integer i less than n such that every integer
    between i and n is composite.

    If skip == 0, the bases used in the probabilistic test are random
    and then the probability that the returned value is composite is
    less than 1/4^abs(count).

    If skip == 1 (the default value) the bases used in the probabilistic
    test are the first abs(count) primes 2, 3, 5, ...

    For other values of skip, the bases used are the abs(count) consecutive
    integer skip, skip + 1, ...

    If modulus = 0, the only values that may be returned are zero and the
    value of residue.  The latter is returned if it is positive, less
    than n, and is such that ptest(residue, count, skip) = 1.

RUNTIME
    The runtime for v = prevcand(n, ...) will depend strongly on the
    number and nature of the integers between n and v.	If this number
    is reasonably large the size of count is largely irrelevant as the
    compositeness of the numbers between n and v will usually be
    determined by the test for small prime factors or one pseudoprime
    test with some base b.  If N > 1, count should be positive so that
    candidates divisible by small primes will be passed over quickly.

    On the average for random n with large word-count N, the runtime
    seems to be between roughly K/N^3 some constant K.

EXAMPLE
    ; print prevcand(50), prevcand(2), prevcand(125,-1), prevcand(125,-2)
    47 1 113 113

    ; print prevcand(100,1,1,1,6), prevcand(100,1,1,-1,6)
    97 89

    ; print prevcand(100,1,1,2,6), prevcand(100,1,1,4,6),
    2 0

    ; print prevcand(100,1,1,53,0), prevcand(100,1,1,53,106)
    53 53

    ; print prevcand(125,1,3), prevcand(125,-1,3), prevcand(125,-2,3)
    113 121 113

    ; print prevcand(2e60, 1, 1, 31, 1e30)
    1999999999999999999999999999914000000000000000000000000000031

LIMITS
    none

LINK LIBRARY
    int zprevcand(ZVALUE n, long count, long skip, ZVALUE res, ZVALUE mod,
	ZVALUE *cand)

SEE ALSO
    factor, isprime, lfactor, nextcand, nextprime, prevprime,
    pfact, pix, ptest

## Copyright (C) 1999-2006  Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License
## as published by the Free Software Foundation.
##
## Calc is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
## Public License for more details.
##
## A copy of version 2.1 of the GNU Lesser General Public License is
## distributed with calc under the filename COPYING-LGPL.  You should have
## received a copy with calc; if not, write to Free Software Foundation, Inc.
## 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##
## @(#) $Revision: 30.1 $
## @(#) $Id: prevcand,v 30.1 2007/03/16 11:10:42 chongo Exp $
## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/prevcand,v $
##
## Under source code control:	1996/02/25 00:27:43
## File existed as early as:	1996
##
## chongo <was here> /\oo/\	http://www.isthe.com/chongo/
## Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
