Skip to content

Commit bbd91d3

Browse files
author
Release Manager
committed
gh-35019: add limit= argument to Integer.prime_divisors()
`Integer.factor()` has an optional `limit=` argument to restrict the search to prime factors up to a certain size. This patch adds a similar argument to the `.prime_divisors()` method. URL: #35019 Reported by: Lorenz Panny Reviewer(s): Edgar Costa, Lorenz Panny
2 parents cf49215 + 805b2de commit bbd91d3

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

build/pkgs/configure/checksums.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
tarball=configure-VERSION.tar.gz
2-
sha1=0897c667d1327d2a51ea3d3bd1d9e1a3f5ca2606
3-
md5=a1f271e5ffcf558d054028839296a072
4-
cksum=437557471
2+
sha1=b26cd9a17917ab6dd2f601e7448929fc9c09767d
3+
md5=4d0234e98408d5a3c6c8b6d5075cc53d
4+
cksum=550848071
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
08185d786047228caff879eba88a1f8148a49020
1+
0ce2ea5d3d899264aef63cea22b27b209b60bec8

src/sage/rings/integer.pyx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,13 +2951,21 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
29512951

29522952
return n
29532953

2954-
def prime_divisors(self):
2954+
def prime_divisors(self, *args, **kwds):
29552955
"""
29562956
Return the prime divisors of this integer, sorted in increasing order.
29572957
29582958
If this integer is negative, we do *not* include -1 among
29592959
its prime divisors, since -1 is not a prime number.
29602960
2961+
INPUT:
2962+
2963+
- ``limit`` -- (integer, optional keyword argument)
2964+
Return only prime divisors up to this bound, and the factorization
2965+
is done by checking primes up to ``limit`` using trial division.
2966+
2967+
Any additional arguments are passed on to the :meth:`factor` method.
2968+
29612969
EXAMPLES::
29622970
29632971
sage: a = 1; a.prime_divisors()
@@ -2968,8 +2976,23 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
29682976
[2, 5]
29692977
sage: a = 2004; a.prime_divisors()
29702978
[2, 3, 167]
2979+
2980+
Setting the optional ``limit`` argument works as expected::
2981+
2982+
sage: a = 10^100 + 1
2983+
sage: a.prime_divisors()
2984+
[73, 137, 401, 1201, 1601, 1676321, 5964848081,
2985+
129694419029057750551385771184564274499075700947656757821537291527196801]
2986+
sage: a.prime_divisors(limit=10^3)
2987+
[73, 137, 401]
2988+
sage: a.prime_divisors(limit=10^7)
2989+
[73, 137, 401, 1201, 1601, 1676321]
29712990
"""
2972-
return [r[0] for r in self.factor()]
2991+
res = [r[0] for r in self.factor(*args, **kwds)]
2992+
limit = kwds.get('limit')
2993+
if limit is not None:
2994+
res = [r for r in res if r <= limit]
2995+
return res
29732996

29742997
prime_factors = prime_divisors
29752998

0 commit comments

Comments
 (0)