Ticket #10784: trac_10784_primes_enhancements.patch

File trac_10784_primes_enhancements.patch, 3.7 KB (added by dsm, 10 years ago)

allow +infinity, proof

  • sage/rings/arith.py

    # HG changeset patch
    # User D. S. McNeil
    # Date 1297828157 -28800
    # Node ID bf7717ce4c65e9d56c07ff1a99d5705925095e8f
    # Parent  f24ce048fa66831e9e99df553e2be055c7081bbe
    Trac 10784: allow infinity as upper bound and proof options in primes()
    
    diff -r f24ce048fa66 -r bf7717ce4c65 sage/rings/arith.py
    a b  
    858858##         X = [a for a in X if a%p != 0]           
    859859##     return P + X                                 
    860860
    861 def primes(start, stop=None):
    862     r"""
    863     Returns an iterator over all primes between start and stop-1,
    864     inclusive. This is much slower than ``prime_range``,
    865     but potentially uses less memory.
    866    
     861def primes(start, stop=None, proof=None):
     862    r""" Returns an iterator over all primes between start and stop-1,
     863    inclusive. This is much slower than ``prime_range``, but
     864    potentially uses less memory.  As with ``next_prime``, the optional
     865    argument proof controls whether the numbers returned are
     866    guaranteed to be prime or not.
     867
    867868    This command is like the xrange command, except it only iterates
    868869    over primes. In some cases it is better to use primes than
    869     prime_range, because primes does not build a list of all primes in
    870     the range in memory all at once. However it is potentially much
    871     slower since it simply calls the ``next_prime``
    872     function repeatedly, and ``next_prime`` is slow,
    873     partly because it proves correctness.
     870    ``prime_range``, because primes does not build a list of all primes in
     871    the range in memory all at once. However, it is potentially much
     872    slower since it simply calls the ``next_prime`` function
     873    repeatedly, and ``next_prime`` is slow.
     874
     875   INPUT:
     876
     877       
     878    -  ``start`` - an integer
     879    lower bound for the primes
     880
     881    -  ``stop`` - an integer (or infinity)
     882    upper (open) bound for the primes
     883
     884    -  ``proof`` - bool or None (default: None)  If True, the function
     885       yields only proven primes.  If False, the function uses a
     886       pseudo-primality test, which is much faster for really big
     887       numbers but does not provide a proof of primality. If None,
     888       uses the global default (see :mod:`sage.structure.proof.proof`)
     889
     890
     891   OUTPUT:
     892
     893   -  an iterator over primes from start to stop-1, inclusive
     894   
    874895   
    875896    EXAMPLES::
    876897   
     
    879900        ...
    880901        5
    881902        7
    882         sage: list(primes(11))
    883         [2, 3, 5, 7]
     903        sage: list(primes(13))
     904        [2, 3, 5, 7, 11]
    884905        sage: list(primes(10000000000, 10000000100))
    885906        [10000000019, 10000000033, 10000000061, 10000000069, 10000000097]
     907        sage: max(primes(10^100, 10^100+10^4, proof=False))
     908        10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009631
     909        sage: next(p for p in primes(10^20, infinity) if is_prime(2*p+1))
     910        100000000000000001243
     911
     912
     913    TESTS::
     914
     915        sage: for a in range(-10, 50):
     916        ...    for b in range(-10, 50):
     917        ...        assert list(primes(a,b)) == list(filter(is_prime, xrange(a,b)))
     918        ...
     919        sage: sum(primes(-10, 9973, proof=False)) == sum(filter(is_prime, range(-10, 9973)))
     920        True
     921        sage: for p in primes(10, infinity):
     922        ...    if p > 20: break
     923        ...    print p
     924        ...
     925        11
     926        13
     927        17
     928        19
     929
    886930    """
    887    
     931    from sage.rings.infinity import infinity
     932
    888933    start = ZZ(start)
    889934    if stop == None:
    890935        stop = start
    891936        start = ZZ(2)
    892     else:
     937    elif stop != infinity:
    893938        stop = ZZ(stop)
    894939    n = start - 1
    895940    while True:
    896         n = next_prime(n)
     941        n = next_prime(n, proof)
    897942        if n < stop:
    898943            yield n
    899944        else: