# 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/sage/rings/arith.py Tue Jan 11 08:10:26 2011 +0100
+++ b/sage/rings/arith.py Wed Feb 16 11:49:17 2011 +0800
@@ 858,19 +858,40 @@
## X = [a for a in X if a%p != 0]
## return P + X
def primes(start, stop=None):
 r"""
 Returns an iterator over all primes between start and stop1,
 inclusive. This is much slower than ``prime_range``,
 but potentially uses less memory.

+def primes(start, stop=None, proof=None):
+ r""" Returns an iterator over all primes between start and stop1,
+ inclusive. This is much slower than ``prime_range``, but
+ potentially uses less memory. As with ``next_prime``, the optional
+ argument proof controls whether the numbers returned are
+ guaranteed to be prime or not.
+
This command is like the xrange command, except it only iterates
over primes. In some cases it is better to use primes than
 prime_range, because primes does not build a list of all primes in
 the range in memory all at once. However it is potentially much
 slower since it simply calls the ``next_prime``
 function repeatedly, and ``next_prime`` is slow,
 partly because it proves correctness.
+ ``prime_range``, because primes does not build a list of all primes in
+ the range in memory all at once. However, it is potentially much
+ slower since it simply calls the ``next_prime`` function
+ repeatedly, and ``next_prime`` is slow.
+
+ INPUT:
+
+
+  ``start``  an integer
+ lower bound for the primes
+
+  ``stop``  an integer (or infinity)
+ upper (open) bound for the primes
+
+  ``proof``  bool or None (default: None) If True, the function
+ yields only proven primes. If False, the function uses a
+ pseudoprimality test, which is much faster for really big
+ numbers but does not provide a proof of primality. If None,
+ uses the global default (see :mod:`sage.structure.proof.proof`)
+
+
+ OUTPUT:
+
+  an iterator over primes from start to stop1, inclusive
+
EXAMPLES::
@@ 879,21 +900,45 @@
...
5
7
 sage: list(primes(11))
 [2, 3, 5, 7]
+ sage: list(primes(13))
+ [2, 3, 5, 7, 11]
sage: list(primes(10000000000, 10000000100))
[10000000019, 10000000033, 10000000061, 10000000069, 10000000097]
+ sage: max(primes(10^100, 10^100+10^4, proof=False))
+ 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009631
+ sage: next(p for p in primes(10^20, infinity) if is_prime(2*p+1))
+ 100000000000000001243
+
+
+ TESTS::
+
+ sage: for a in range(10, 50):
+ ... for b in range(10, 50):
+ ... assert list(primes(a,b)) == list(filter(is_prime, xrange(a,b)))
+ ...
+ sage: sum(primes(10, 9973, proof=False)) == sum(filter(is_prime, range(10, 9973)))
+ True
+ sage: for p in primes(10, infinity):
+ ... if p > 20: break
+ ... print p
+ ...
+ 11
+ 13
+ 17
+ 19
+
"""

+ from sage.rings.infinity import infinity
+
start = ZZ(start)
if stop == None:
stop = start
start = ZZ(2)
 else:
+ elif stop != infinity:
stop = ZZ(stop)
n = start  1
while True:
 n = next_prime(n)
+ n = next_prime(n, proof)
if n < stop:
yield n
else: