Set and subsets of prime numbers - Sets

Skip to content
Back to top
View this page
Edit this page

# Set and subsets of prime numbers

AUTHORS:

> - William Stein (2005): original version

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

> - Xavier Caruso (2025-10): implement congruence conditions

class sage.sets.primes.Primes(modulus, classes, exceptions)

  Bases: `Set_generic`, `UniqueRepresentation`

  The set of prime numbers and some of its subsets.

  EXAMPLES:

  The set of all primes numbers:

  Sage

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

  Python

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

  The arguments `modulus` and `classes` allows for constructing subsets given by congruence conditions:

  Sage

      sage: Primes(modulus=4)
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...

  [Python]

      >>> from sage.all import *
      >>> Primes(modulus=Integer(4))
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...

  By default the congruence class \(1\) is selected, but we can specify any subset of congruence classes:

  Sage

      sage: Primes(modulus=4, classes=[3])
      Set of prime numbers congruent to 3 modulo 4: 3, 7, 11, 19, ...
      sage: Primes(modulus=8, classes=[1, 3])
      Set of prime numbers congruent to 1, 3 modulo 8: 3, 11, 17, 19, ...

  Python

      >>> from sage.all import *
      >>> Primes(modulus=Integer(4), classes=[Integer(3)])
      Set of prime numbers congruent to 3 modulo 4: 3, 7, 11, 19, ...
      >>> Primes(modulus=Integer(8), classes=[Integer(1), Integer(3)])
      Set of prime numbers congruent to 1, 3 modulo 8: 3, 11, 17, 19, ...

  If possible, the congruence conditions are simplified:

  Sage

      sage: Primes(modulus=8, classes=[1, 5])
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...

  [Python]

      >>> from sage.all import *
      >>> Primes(modulus=Integer(8), classes=[Integer(1), Integer(5)])
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...

  We can create a finite set of primes by passing in `modulus=0`:

  Sage

      sage: Primes(modulus=0, classes=[2, 3, 5, 11])
      Finite set of prime numbers: 2, 3, 5, 11

  Python

      >>> from sage.all import *
      >>> Primes(modulus=Integer(0), classes=[Integer(2), Integer(3), Integer(5), Integer(11)])
      Finite set of prime numbers: 2, 3, 5, 11

  We show various operations that can be performed on these sets:

  Sage

      sage: P = Primes(modulus=4); P
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
      sage: P.cardinality()
      +Infinity
      sage: P[:10]
      [5, 13, 17, 29, 37, 41, 53, 61, 73, 89]
      sage: P.next(500)
      509

  [Python]

      >>> from sage.all import *
      >>> P = Primes(modulus=Integer(4)); P
      Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
      >>> P.cardinality()
      +Infinity
      >>> P[:Integer(10)]
      [5, 13, 17, 29, 37, 41, 53, 61, 73, 89]
      >>> P.next(Integer(500))
      509

  Sage

      sage: Q = Primes(modulus=4, classes=[3])
      sage: PQ = P.union(Q)
      sage: PQ
      Set of all prime numbers with 2 excluded: 3, 5, 7, 11, ...
      sage: PQ.complement_in_primes()
      Finite set of prime numbers: 2
      sage: PQ.complement_in_primes().cardinality()
      1

  Python

      >>> from sage.all import *
      >>> Q = Primes(modulus=Integer(4), classes=[Integer(3)])
      >>> PQ = P.union(Q)
      >>> PQ
      Set of all prime numbers with 2 excluded: 3, 5, 7, 11, ...
      >>> PQ.complement_in_primes()
      Finite set of prime numbers: 2
      >>> PQ.complement_in_primes().cardinality()
      1

  cardinality()

    Return the cardinality of this set.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.cardinality()
        +Infinity

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.cardinality()
        +Infinity

    Sage

        sage: P = Primes(modulus=4, classes=[2]); P
        Finite set of prime numbers: 2
        sage: P.cardinality()
        1

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4), classes=[Integer(2)]); P
        Finite set of prime numbers: 2
        >>> P.cardinality()
        1

  complement_in_primes()

    Return the complement of this set in the set of all prime numbers.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: Q = P.complement_in_primes(); Q
        Set of prime numbers congruent to 3 modulo 4 with 2 included: 2, 3, 7, 11, ...

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> Q = P.complement_in_primes(); Q
        Set of prime numbers congruent to 3 modulo 4 with 2 included: 2, 3, 7, 11, ...

    We check that the union of \(P\) and \(Q\) is the whole set of prime numbers:

    Sage

        sage: P.union(Q)
        Set of all prime numbers: 2, 3, 5, 7, ...

    [Python]

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

    and that the intersection is empty:

    Sage

        sage: P.intersection(Q)
        Empty set of prime numbers

    Python

        >>> from sage.all import *
        >>> P.intersection(Q)
        Empty set of prime numbers

    See also

    `intersection()`, `union()`

  congruence_classes()

    Return the congruence classes selected in the subset of prime numbers.

    OUTPUT:

    A pair `(modulus, list of classes)`

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4)
        sage: P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.congruence_classes()
        (4, [1])

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4))
        >>> P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.congruence_classes()
        (4, [1])

    If possible, the congruence classes are simplified:

    Sage

        sage: P = Primes(modulus=10, classes=[1, 3])
        sage: P
        Set of prime numbers congruent to 1, 3 modulo 5: 3, 11, 13, 23, ...
        sage: P.congruence_classes()
        (5, [1, 3])

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(10), classes=[Integer(1), Integer(3)])
        >>> P
        Set of prime numbers congruent to 1, 3 modulo 5: 3, 11, 13, 23, ...
        >>> P.congruence_classes()
        (5, [1, 3])

    If this subset is finite, the output of this method is always \((1, [])\). The elements of the subset can be retrieved using the method `list()` or `included()`:

    Sage

        sage: P = Primes(modulus=0, classes=range(50))
        sage: P
        Finite set of prime numbers: 2, 3, 5, 7, ..., 43, 47
        sage: P.congruence_classes()
        (1, [])
        sage: list(P)
        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(0), classes=range(Integer(50)))
        >>> P
        Finite set of prime numbers: 2, 3, 5, 7, ..., 43, 47
        >>> P.congruence_classes()
        (1, [])
        >>> list(P)
        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

    See also

    `included()`, `excluded()`

  density()

    Return the density of this set in the set of all prime numbers.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.density()
        1/2

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.density()
        1/2

  exclude(elements)

    Return this set with the integers in `elements` excluded.

    INPUT:

    - `elements` – an integer, or a tuple/list of integers

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.exclude(11)
        Set of prime numbers congruent to 1 modulo 5 with 11 excluded: 31, 41, 61, 71, ...
        sage: P.exclude([11, 31])
        Set of prime numbers congruent to 1 modulo 5 with 11, 31 excluded: 41, 61, 71, 101, ...

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.exclude(Integer(11))
        Set of prime numbers congruent to 1 modulo 5 with 11 excluded: 31, 41, 61, 71, ...
        >>> P.exclude([Integer(11), Integer(31)])
        Set of prime numbers congruent to 1 modulo 5 with 11, 31 excluded: 41, 61, 71, 101, ...

    If we try to exclude an element which is not in the set, nothing changes:

    Sage

        sage: P.exclude(2)
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...

    [Python]

        >>> from sage.all import *
        >>> P.exclude(Integer(2))
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...

    See also

    `include()`

  excluded()

    Return the list of elements which are excluded, that are the elements in the congruence classes defining this subset but not in this subset.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4)
        sage: P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.excluded()
        []

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4))
        >>> P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.excluded()
        []

    Sage

        sage: Q = P.exclude(5)
        sage: Q
        Set of prime numbers congruent to 1 modulo 4 with 5 excluded: 13, 17, 29, 37, ...
        sage: Q.excluded()
        [5]

    [Python]

        >>> from sage.all import *
        >>> Q = P.exclude(Integer(5))
        >>> Q
        Set of prime numbers congruent to 1 modulo 4 with 5 excluded: 13, 17, 29, 37, ...
        >>> Q.excluded()
        [5]

    See also

    `included()`, `congruence_classes()`

  first(n=None)

    Return the first element in this set.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.first()
        11

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.first()
        11

  include(elements, check=True)

    Return this set with the integers in `elements` included.

    INPUT:

    - `elements` – an integer, or a tuple/list of integers

    - `check` – a boolean (default: `True`); if `False`, do not raise an error if we try to add composite numbers

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.include(2)
        Set of prime numbers congruent to 1 modulo 5 with 2 included: 2, 11, 31, 41, ...
        sage: P.include([2, 3])
        Set of prime numbers congruent to 1 modulo 5 with 2, 3 included: 2, 3, 11, 31, ...

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.include(Integer(2))
        Set of prime numbers congruent to 1 modulo 5 with 2 included: 2, 11, 31, 41, ...
        >>> P.include([Integer(2), Integer(3)])
        Set of prime numbers congruent to 1 modulo 5 with 2, 3 included: 2, 3, 11, 31, ...

    If we try to include an element which is already in the set, nothing changes:

    Sage

        sage: P.include(11)
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...

    [Python]

        >>> from sage.all import *
        >>> P.include(Integer(11))
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...

    Trying to include a composite number results in an error:

    Sage

        sage: P.include(10)
        Traceback (most recent call last):
        ...
        ValueError: 10 is not a prime number

    Python

        >>> from sage.all import *
        >>> P.include(Integer(10))
        Traceback (most recent call last):
        ...
        ValueError: 10 is not a prime number

    We can avoid this by passing in `check=False`; in this case, composite numbers are however not added to the set. This behavior can be convenient if one wants to add all prime numbers in a range:

    Sage

        sage: P.include(range(20, 30), check=False)
        Set of prime numbers congruent to 1 modulo 5 with 23, 29 included: 11, 23, 29, 31, ...

    [Python]

        >>> from sage.all import *
        >>> P.include(range(Integer(20), Integer(30)), check=False)
        Set of prime numbers congruent to 1 modulo 5 with 23, 29 included: 11, 23, 29, 31, ...

    See also

    `exclude()`

  included()

    Return the list of elements which are additionally included (that are, outside the congruence classes) to this set.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4)
        sage: P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.included()
        []

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4))
        >>> P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.included()
        []

    Sage

        sage: Q = P.include(2)
        sage: Q
        Set of prime numbers congruent to 1 modulo 4 with 2 included: 2, 5, 13, 17, ...
        sage: Q.included()
        [2]

    [Python]

        >>> from sage.all import *
        >>> Q = P.include(Integer(2))
        >>> Q
        Set of prime numbers congruent to 1 modulo 4 with 2 included: 2, 5, 13, 17, ...
        >>> Q.included()
        [2]

    See also

    `excluded()`, `congruence_classes()`

  intersection(other)

    Return the intersection of this set with `other`.

    INPUT:

    - `other` – a subset of the set of prime numbers

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: Q = Primes(modulus=3, classes=[2]); Q
        Set of prime numbers congruent to 2 modulo 3: 2, 5, 11, 17, ...
        sage: P.intersection(Q)
        Set of prime numbers congruent to 11 modulo 15: 11, 41, 71, 101, ...

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> Q = Primes(modulus=Integer(3), classes=[Integer(2)]); Q
        Set of prime numbers congruent to 2 modulo 3: 2, 5, 11, 17, ...
        >>> P.intersection(Q)
        Set of prime numbers congruent to 11 modulo 15: 11, 41, 71, 101, ...

    It is also possible to take the intersection with a range:

    Sage

        sage: P.intersection(range(100))
        Finite set of prime numbers: 11, 31, 41, 61, 71

    [Python]

        >>> from sage.all import *
        >>> P.intersection(range(Integer(100)))
        Finite set of prime numbers: 11, 31, 41, 61, 71

    See also

    `complement_in_primes()`, `union()`

  is_almost_equal(other)

    Return whether this set only differs from `other` by a finite set.

    INPUT:

    - `other` – a subset of the set of prime numbers

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=20, classes=[1, 2]); P
        Set of prime numbers congruent to 1 modulo 20 with 2 included: 2, 41, 61, 101, ...
        sage: Q = Primes(modulus=20, classes=[1, 5]); Q
        Set of prime numbers congruent to 1 modulo 20 with 5 included: 5, 41, 61, 101, ...
        sage: P.is_almost_equal(Q)
        True

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(20), classes=[Integer(1), Integer(2)]); P
        Set of prime numbers congruent to 1 modulo 20 with 2 included: 2, 41, 61, 101, ...
        >>> Q = Primes(modulus=Integer(20), classes=[Integer(1), Integer(5)]); Q
        Set of prime numbers congruent to 1 modulo 20 with 5 included: 5, 41, 61, 101, ...
        >>> P.is_almost_equal(Q)
        True

    Sage

        sage: R = Primes(modulus=10); R
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.is_almost_equal(R)
        False

    [Python]

        >>> from sage.all import *
        >>> R = Primes(modulus=Integer(10)); R
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.is_almost_equal(R)
        False

  is_cofinite()

    Return `True` if this set is cofinite in the set of all prime numbers; `False` otherwise.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: P.is_cofinite()
        False

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> P.is_cofinite()
        False

    Sage

        sage: P = Primes(modulus=4, classes=[1, 3]); P
        Set of all prime numbers with 2 excluded: 3, 5, 7, 11, ...
        sage: P.is_cofinite()
        True

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4), classes=[Integer(1), Integer(3)]); P
        Set of all prime numbers with 2 excluded: 3, 5, 7, 11, ...
        >>> P.is_cofinite()
        True

    See also

    `is_empty()`, `is_finite()`

  is_disjoint(other, almost=False)

    Return whether the intersection of this set with `other` is empty (or finite, if `almost` is `True`).

    INPUT:

    - `other` – a subset of the set of prime numbers

    - `almost` – a boolean (default: `False`)

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: Q = Primes(modulus=4, classes=[3]); Q
        Set of prime numbers congruent to 3 modulo 4: 3, 7, 11, 19, ...
        sage: P.is_disjoint(Q)
        True

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> Q = Primes(modulus=Integer(4), classes=[Integer(3)]); Q
        Set of prime numbers congruent to 3 modulo 4: 3, 7, 11, 19, ...
        >>> P.is_disjoint(Q)
        True

    Sage

        sage: R = Primes(modulus=5, classes=[3]); R
        Set of prime numbers congruent to 3 modulo 5: 3, 13, 23, 43, ...
        sage: P.is_disjoint(R)
        False
        sage: Q.is_disjoint(R)
        False

    [Python]

        >>> from sage.all import *
        >>> R = Primes(modulus=Integer(5), classes=[Integer(3)]); R
        Set of prime numbers congruent to 3 modulo 5: 3, 13, 23, 43, ...
        >>> P.is_disjoint(R)
        False
        >>> Q.is_disjoint(R)
        False

    We illustrate the behavior when `almost=True`:

    Sage

        sage: Q5 = Q.include(5); Q5
        Set of prime numbers congruent to 3 modulo 4 with 5 included: 3, 5, 7, 11, ...
        sage: P.is_disjoint(Q5)
        False
        sage: P.is_disjoint(Q5, almost=True)
        True

    Python

        >>> from sage.all import *
        >>> Q5 = Q.include(Integer(5)); Q5
        Set of prime numbers congruent to 3 modulo 4 with 5 included: 3, 5, 7, 11, ...
        >>> P.is_disjoint(Q5)
        False
        >>> P.is_disjoint(Q5, almost=True)
        True

    See also

    `is_subset()`, `is_disjoint()`, `is_almost_equal()`

  is_empty()

    Return `True` if this set is empty; `False` otherwise.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=6); P
        Set of prime numbers congruent to 1 modulo 3: 7, 13, 19, 31, ...
        sage: P.is_empty()
        False

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(6)); P
        Set of prime numbers congruent to 1 modulo 3: 7, 13, 19, 31, ...
        >>> P.is_empty()
        False

    Sage

        sage: P = Primes(modulus=6, classes=[6]); P
        Empty set of prime numbers
        sage: P.is_empty()
        True

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(6), classes=[Integer(6)]); P
        Empty set of prime numbers
        >>> P.is_empty()
        True

    See also

    `is_finite()`, `is_cofinite()`

  is_finite()

    Return `True` if this set is finite; `False` otherwise.

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.is_finite()
        False

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.is_finite()
        False

    Sage

        sage: P = Primes(modulus=0, classes=[2, 5]); P
        Finite set of prime numbers: 2, 5
        sage: P.is_finite()
        True

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(0), classes=[Integer(2), Integer(5)]); P
        Finite set of prime numbers: 2, 5
        >>> P.is_finite()
        True

    See also

    `is_empty()`, `is_cofinite()`

  is_subset(other, almost=False)

    Return whether this set is a subset of `other`.

    INPUT:

    - `other` – a subset of the set of prime numbers

    - `almost` – a boolean (default: `False`); if `True`, the inclusion is only checked up to a finite set

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: Q = Primes(modulus=8); Q
        Set of prime numbers congruent to 1 modulo 8: 17, 41, 73, 89, ...
        sage: P.is_subset(Q)
        False
        sage: Q.is_subset(P)
        True

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> Q = Primes(modulus=Integer(8)); Q
        Set of prime numbers congruent to 1 modulo 8: 17, 41, 73, 89, ...
        >>> P.is_subset(Q)
        False
        >>> Q.is_subset(P)
        True

    When `almost=True`, the inclusion is only checked up to a finite set:

    Sage

        sage: Q2 = Q.include(2); Q2
        Set of prime numbers congruent to 1 modulo 8 with 2 included: 2, 17, 41, 73, ...
        sage: Q2.is_subset(P)
        False
        sage: Q2.is_subset(P, almost=True)
        True

    [Python]

        >>> from sage.all import *
        >>> Q2 = Q.include(Integer(2)); Q2
        Set of prime numbers congruent to 1 modulo 8 with 2 included: 2, 17, 41, 73, ...
        >>> Q2.is_subset(P)
        False
        >>> Q2.is_subset(P, almost=True)
        True

    See also

    `is_superset()`, `is_disjoint()`, `is_almost_equal()`

  is_superset(other, almost=False)

    Return whether this set contains the set `other` as a subset.

    INPUT:

    - `other` – a subset of the set of prime numbers

    - `almost` – a boolean (default: `False`); if `True`, the inclusion is only checked up to a finite set

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=4); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        sage: Q = Primes(modulus=8); Q
        Set of prime numbers congruent to 1 modulo 8: 17, 41, 73, 89, ...
        sage: P.is_superset(Q)
        True
        sage: Q.is_superset(P)
        False

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(4)); P
        Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
        >>> Q = Primes(modulus=Integer(8)); Q
        Set of prime numbers congruent to 1 modulo 8: 17, 41, 73, 89, ...
        >>> P.is_superset(Q)
        True
        >>> Q.is_superset(P)
        False

    When `almost=True`, the inclusion is only checked up to a finite set:

    Sage

        sage: Q2 = Q.include(2); Q2
        Set of prime numbers congruent to 1 modulo 8 with 2 included: 2, 17, 41, 73, ...
        sage: P.is_superset(Q2)
        False
        sage: P.is_superset(Q2, almost=True)
        True

    [Python]

        >>> from sage.all import *
        >>> Q2 = Q.include(Integer(2)); Q2
        Set of prime numbers congruent to 1 modulo 8 with 2 included: 2, 17, 41, 73, ...
        >>> P.is_superset(Q2)
        False
        >>> P.is_superset(Q2, almost=True)
        True

    See also

    `is_subset()`, `is_disjoint()`, `is_almost_equal()`

  next(x)

    Return the smallest element in this set strictly greater than `x`.

    INPUT:

    - `x` – an integer

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P.next(1000)
        1021

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P.next(Integer(1000))
        1021

    If there is no element greater than the given bound, an error is raised:

    Sage

        sage: P = Primes(modulus=0, classes=[2, 5]); P
        Finite set of prime numbers: 2, 5
        sage: P.next(10)
        Traceback (most recent call last):
        ...
        ValueError: no element greater that 10 in this set

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(0), classes=[Integer(2), Integer(5)]); P
        Finite set of prime numbers: 2, 5
        >>> P.next(Integer(10))
        Traceback (most recent call last):
        ...
        ValueError: no element greater that 10 in this set

  union(other)

    Return the union of this set and `other`.

    INPUT:

    - `other` – a subset of the set of prime numbers

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: Q = Primes(modulus=3, classes=[2]); Q
        Set of prime numbers congruent to 2 modulo 3: 2, 5, 11, 17, ...
        sage: P.union(Q)
        Set of prime numbers congruent to 1, 2, 8, 11, 14 modulo 15 with 5 included: 2, 5, 11, 17, ...

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> Q = Primes(modulus=Integer(3), classes=[Integer(2)]); Q
        Set of prime numbers congruent to 2 modulo 3: 2, 5, 11, 17, ...
        >>> P.union(Q)
        Set of prime numbers congruent to 1, 2, 8, 11, 14 modulo 15 with 5 included: 2, 5, 11, 17, ...

    See also

    `complement_in_primes()`, `intersection()`

  unrank(n)

    Return the `n`-th element of this set.

    INPUT:

    - `n` – an integer

    EXAMPLES:

    Sage

        sage: P = Primes(modulus=5); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        sage: P[0]   # indirect doctest
        11
        sage: P[10]  # indirect doctest
        211

    Python

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(5)); P
        Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
        >>> P[Integer(0)]   # indirect doctest
        11
        >>> P[Integer(10)]  # indirect doctest
        211

    If there is less than \(n\) elements in this set, an error is raised:

    Sage

        sage: P = Primes(modulus=0, classes=[2, 5]); P
        Finite set of prime numbers: 2, 5
        sage: P[1]
        5
        sage: P[2]  # indirect doctest
        Traceback (most recent call last):
        ...
        IndexError: this set has not enough elements

    [Python]

        >>> from sage.all import *
        >>> P = Primes(modulus=Integer(0), classes=[Integer(2), Integer(5)]); P
        Finite set of prime numbers: 2, 5
        >>> P[Integer(1)]
        5
        >>> P[Integer(2)]  # indirect doctest
        Traceback (most recent call last):
        ...
        IndexError: this set has not enough elements