Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/sets/primes.py
4054 views
1
"""
2
The set of prime numbers
3
4
AUTHORS:
5
6
- William Stein (2005): original version
7
- Florent Hivert (2009-11): adapted to the category framework. The following
8
methods were removed:
9
10
- cardinality, __len__, __iter__: provided by EnumeratedSets
11
- __cmp__(self, other): __eq__ is provided by UniqueRepresentation
12
and seems to do as good a job (all test pass)
13
"""
14
#*****************************************************************************
15
# Copyright (C) 2005 William Stein <[email protected]>
16
# 2009 Florent Hivert <[email protected]>
17
#
18
# Distributed under the terms of the GNU General Public License (GPL)
19
#
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
23
from sage.rings.all import ZZ
24
from set import Set_generic
25
from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
26
from sage.rings.arith import nth_prime
27
from sage.structure.unique_representation import UniqueRepresentation
28
29
class Primes(Set_generic, UniqueRepresentation):
30
"""
31
The set of prime numbers.
32
33
EXAMPLES::
34
35
sage: P = Primes(); P
36
Set of all prime numbers: 2, 3, 5, 7, ...
37
38
We show various operations on the set of prime numbers::
39
40
sage: P.cardinality()
41
+Infinity
42
sage: R = Primes()
43
sage: P == R
44
True
45
sage: 5 in P
46
True
47
sage: 100 in P
48
False
49
50
sage: len(P) # note: this used to be a TypeError
51
Traceback (most recent call last):
52
...
53
NotImplementedError: infinite list
54
"""
55
56
@staticmethod
57
def __classcall__(cls, proof=True):
58
"""
59
TESTS::
60
61
sage: Primes(proof=True) is Primes()
62
True
63
sage: Primes(proof=False) is Primes()
64
False
65
"""
66
return super(Primes, cls).__classcall__(cls, proof)
67
68
def __init__(self, proof):
69
"""
70
EXAMPLES::
71
72
sage: P = Primes(); P
73
Set of all prime numbers: 2, 3, 5, 7, ...
74
75
sage: Q = Primes(proof = False); Q
76
Set of all prime numbers: 2, 3, 5, 7, ...
77
78
TESTS::
79
80
sage: P.category()
81
Category of facade infinite enumerated sets
82
sage: TestSuite(P).run()
83
84
sage: Q.category()
85
Category of facade infinite enumerated sets
86
sage: TestSuite(Q).run()
87
88
The set of primes can be compared to various things,
89
but is only equal to itself::
90
91
sage: P = Primes()
92
sage: R = Primes()
93
sage: cmp(P,R)
94
0
95
sage: P == R
96
True
97
sage: P != R
98
False
99
sage: Q=[1,2,3]
100
sage: Q != P # indirect doctest
101
True
102
sage: R.<x>=ZZ[]
103
sage: P!=x^2+x
104
True
105
106
Make sure changing order changes the comparison with something
107
of a different type::
108
109
sage: cmp('foo', Primes()) != cmp(Primes(), 'foo')
110
True
111
"""
112
super(Primes, self).__init__(facade = ZZ, category = InfiniteEnumeratedSets())
113
self.__proof = proof
114
115
def _repr_(self):
116
"""
117
Representation of the set of primes.
118
119
EXAMPLES::
120
121
sage: P = Primes(); P
122
Set of all prime numbers: 2, 3, 5, 7, ...
123
"""
124
return "Set of all prime numbers: 2, 3, 5, 7, ..."
125
126
def __contains__(self, x):
127
"""
128
Checks whether an object is a prime number.
129
130
EXAMPLES::
131
132
sage: P = Primes()
133
sage: 5 in P
134
True
135
sage: 100 in P
136
False
137
sage: 1.5 in P
138
False
139
sage: e in P
140
False
141
"""
142
try:
143
if not x in ZZ:
144
return False
145
return ZZ(x).is_prime()
146
except TypeError:
147
return False
148
149
def _an_element_(self):
150
"""
151
Returns a typical prime number.
152
153
EXAMPLES::
154
155
sage: P = Primes()
156
sage: P._an_element_()
157
43
158
"""
159
return ZZ(43)
160
161
def first(self):
162
"""
163
Returns the first prime number.
164
165
EXAMPLES::
166
167
sage: P = Primes()
168
sage: P.first()
169
2
170
"""
171
return ZZ(2)
172
173
def next(self, pr):
174
"""
175
Returns the next prime number.
176
177
EXAMPLES::
178
179
sage: P = Primes()
180
sage: P.next(5)
181
7
182
"""
183
pr = pr.next_prime(self.__proof)
184
return pr
185
186
def unrank(self, n):
187
"""
188
Returns the n-th prime number.
189
190
EXAMPLES::
191
sage: P = Primes()
192
sage: P.unrank(0)
193
2
194
sage: P.unrank(5)
195
13
196
sage: P.unrank(42)
197
191
198
"""
199
return nth_prime(n+1)
200
201