The set of prime numbers

AUTHORS:

  • William Stein (2005): original version

  • Florent Hivert (2009-11): adapted to the category framework.

class sage.sets.primes.Primes(proof)[source]

Bases: Set_generic, UniqueRepresentation

The set of prime numbers.

EXAMPLES:

sage: P = Primes(); P
Set of all prime numbers: 2, 3, 5, 7, ...
>>> from sage.all import *
>>> P = Primes(); P
Set of all prime numbers: 2, 3, 5, 7, ...
P = Primes(); P

We show various operations on the set of prime numbers:

sage: P.cardinality()
+Infinity
sage: R = Primes()
sage: P == R
True
sage: 5 in P
True
sage: 100 in P
False

sage: len(P)
Traceback (most recent call last):
...
NotImplementedError: infinite set
>>> from sage.all import *
>>> P.cardinality()
+Infinity
>>> R = Primes()
>>> P == R
True
>>> Integer(5) in P
True
>>> Integer(100) in P
False

>>> len(P)
Traceback (most recent call last):
...
NotImplementedError: infinite set
P.cardinality()
R = Primes()
P == R
5 in P
100 in P
len(P)
first()[source]

Return the first prime number.

EXAMPLES:

sage: P = Primes()
sage: P.first()
2
>>> from sage.all import *
>>> P = Primes()
>>> P.first()
2
P = Primes()
P.first()
next(pr)[source]

Return the next prime number.

EXAMPLES:

sage: P = Primes()
sage: P.next(5)                                                             # needs sage.libs.pari
7
>>> from sage.all import *
>>> P = Primes()
>>> P.next(Integer(5))                                                             # needs sage.libs.pari
7
P = Primes()
P.next(5)                                                             # needs sage.libs.pari
unrank(n)[source]

Return the n-th prime number.

EXAMPLES:

sage: P = Primes()
sage: P.unrank(0)                                                           # needs sage.libs.pari
2
sage: P.unrank(5)                                                           # needs sage.libs.pari
13
sage: P.unrank(42)                                                          # needs sage.libs.pari
191
>>> from sage.all import *
>>> P = Primes()
>>> P.unrank(Integer(0))                                                           # needs sage.libs.pari
2
>>> P.unrank(Integer(5))                                                           # needs sage.libs.pari
13
>>> P.unrank(Integer(42))                                                          # needs sage.libs.pari
191
P = Primes()
P.unrank(0)                                                           # needs sage.libs.pari
P.unrank(5)                                                           # needs sage.libs.pari
P.unrank(42)                                                          # needs sage.libs.pari