Skip to content

Commit 2ddb381

Browse files
author
Xavier Caruso
committed
method in_range
1 parent 31525d0 commit 2ddb381

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/sage/sets/primes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,35 @@ def _an_element_(self):
547547
raise ValueError("this set is empty")
548548
return self.next(42)
549549

550+
def in_range(self, start, stop=None):
551+
r"""
552+
Return the list of the elements of this set which are
553+
in the given range.
554+
555+
EXAMPLES::
556+
557+
sage: P = Primes(modulus=3); P
558+
Set of prime numbers congruent to 1 modulo 3: 7, 13, 19, 31, ...
559+
sage: P.in_range(50, 100)
560+
[61, 67, 73, 79, 97]
561+
562+
When a unique integer is passed, it is interpreted as the
563+
upper bound::
564+
565+
sage: P.in_range(50)
566+
[7, 13, 19, 31, 37, 43]
567+
"""
568+
if stop is None:
569+
stop = start
570+
start = 1
571+
elements = []
572+
x = start - 1
573+
while True:
574+
x = self.next(x)
575+
if x >= stop:
576+
return elements
577+
elements.append(x)
578+
550579
def unrank(self, n):
551580
r"""
552581
Return the ``n``-th element of this set.

0 commit comments

Comments
 (0)