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)[source]

Bases: Set_generic, UniqueRepresentation

The set of prime numbers and some of its subsets.

EXAMPLES:

The set of all primes numbers:

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, ...

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

sage: Primes(modulus=4)
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
>>> 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: 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, ...
>>> 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: Primes(modulus=8, classes=[1, 5])
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
>>> 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: Primes(modulus=0, classes=[2, 3, 5, 11])
Finite set of prime numbers: 2, 3, 5, 11
>>> 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: 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
>>> 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: 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
>>> 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()[source]

Return the cardinality of this set.

EXAMPLES:

sage: P = Primes(modulus=4); P
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
sage: P.cardinality()
+Infinity
>>> 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: P = Primes(modulus=4, classes=[2]); P
Finite set of prime numbers: 2
sage: P.cardinality()
1
>>> 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()[source]

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

EXAMPLES:

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, ...
>>> 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: P.union(Q)
Set of all prime numbers: 2, 3, 5, 7, ...
>>> from sage.all import *
>>> P.union(Q)
Set of all prime numbers: 2, 3, 5, 7, ...

and that the intersection is empty:

sage: P.intersection(Q)
Empty set of prime numbers
>>> from sage.all import *
>>> P.intersection(Q)
Empty set of prime numbers
congruence_classes()[source]

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

OUTPUT:

A pair (modulus, list of classes)

EXAMPLES:

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])
>>> 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: 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])
>>> 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: 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]
>>> 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]
density()[source]

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

EXAMPLES:

sage: P = Primes(modulus=4); P
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
sage: P.density()
1/2
>>> 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)[source]

Return this set with the integers in elements excluded.

INPUT:

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

EXAMPLES:

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, ...
>>> 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: P.exclude(2)
Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
>>> 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()[source]

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: P = Primes(modulus=4)
sage: P
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
sage: P.excluded()
[]
>>> 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: 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]
>>> 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]
first(n=None)[source]

Return the first element in this set.

EXAMPLES:

sage: P = Primes(modulus=5); P
Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
sage: P.first()
11
>>> 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)[source]

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: 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, ...
>>> 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: P.include(11)
Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
>>> 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: P.include(10)
Traceback (most recent call last):
...
ValueError: 10 is not a prime number
>>> 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: P.include(range(20, 30), check=False)
Set of prime numbers congruent to 1 modulo 5 with 23, 29 included: 11, 23, 29, 31, ...
>>> 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()[source]

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

EXAMPLES:

sage: P = Primes(modulus=4)
sage: P
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
sage: P.included()
[]
>>> 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: 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]
>>> 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]
intersection(other)[source]

Return the intersection of this set with other.

INPUT:

  • other – a subset of the set of prime numbers

EXAMPLES:

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, ...
>>> 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: P.intersection(range(100))
Finite set of prime numbers: 11, 31, 41, 61, 71
>>> from sage.all import *
>>> P.intersection(range(Integer(100)))
Finite set of prime numbers: 11, 31, 41, 61, 71
is_almost_equal(other)[source]

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

INPUT:

  • other – a subset of the set of prime numbers

EXAMPLES:

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
>>> 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: 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
>>> 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()[source]

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

EXAMPLES:

sage: P = Primes(modulus=4); P
Set of prime numbers congruent to 1 modulo 4: 5, 13, 17, 29, ...
sage: P.is_cofinite()
False
>>> 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: 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
>>> 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
is_disjoint(other, almost=False)[source]

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: 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
>>> 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: 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
>>> 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: 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
>>> 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
is_empty()[source]

Return True if this set is empty; False otherwise.

EXAMPLES:

sage: P = Primes(modulus=6); P
Set of prime numbers congruent to 1 modulo 3: 7, 13, 19, 31, ...
sage: P.is_empty()
False
>>> 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: P = Primes(modulus=6, classes=[6]); P
Empty set of prime numbers
sage: P.is_empty()
True
>>> from sage.all import *
>>> P = Primes(modulus=Integer(6), classes=[Integer(6)]); P
Empty set of prime numbers
>>> P.is_empty()
True
is_finite()[source]

Return True if this set is finite; False otherwise.

EXAMPLES:

sage: P = Primes(modulus=5); P
Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
sage: P.is_finite()
False
>>> 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: P = Primes(modulus=0, classes=[2, 5]); P
Finite set of prime numbers: 2, 5
sage: P.is_finite()
True
>>> 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
is_subset(other, almost=False)[source]

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: 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
>>> 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: 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
>>> 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
is_superset(other, almost=False)[source]

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: 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
>>> 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: 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
>>> 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
next(x)[source]

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

INPUT:

  • x – an integer

EXAMPLES:

sage: P = Primes(modulus=5); P
Set of prime numbers congruent to 1 modulo 5: 11, 31, 41, 61, ...
sage: P.next(1000)
1021
>>> 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: 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
>>> 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)[source]

Return the union of this set and other.

INPUT:

  • other – a subset of the set of prime numbers

EXAMPLES:

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, ...
>>> 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, ...
unrank(n)[source]

Return the n-th element of this set.

INPUT:

  • n – an integer

EXAMPLES:

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
>>> 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: 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
>>> 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