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 | | |
| 861 | def 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 | |
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 | |
| 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 | |