Skip to content

Commit

Permalink
sagemathgh-35019: add limit= argument to Integer.prime_divisors()
Browse files Browse the repository at this point in the history
`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: sagemath#35019
Reported by: Lorenz Panny
Reviewer(s): Edgar Costa, Lorenz Panny
  • Loading branch information
Release Manager committed Feb 22, 2023
2 parents cf49215 + 805b2de commit bbd91d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tarball=configure-VERSION.tar.gz
sha1=0897c667d1327d2a51ea3d3bd1d9e1a3f5ca2606
md5=a1f271e5ffcf558d054028839296a072
cksum=437557471
sha1=b26cd9a17917ab6dd2f601e7448929fc9c09767d
md5=4d0234e98408d5a3c6c8b6d5075cc53d
cksum=550848071
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
08185d786047228caff879eba88a1f8148a49020
0ce2ea5d3d899264aef63cea22b27b209b60bec8
27 changes: 25 additions & 2 deletions src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2951,13 +2951,21 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):

return n

def prime_divisors(self):
def prime_divisors(self, *args, **kwds):
"""
Return the prime divisors of this integer, sorted in increasing order.
If this integer is negative, we do *not* include -1 among
its prime divisors, since -1 is not a prime number.
INPUT:
- ``limit`` -- (integer, optional keyword argument)
Return only prime divisors up to this bound, and the factorization
is done by checking primes up to ``limit`` using trial division.
Any additional arguments are passed on to the :meth:`factor` method.
EXAMPLES::
sage: a = 1; a.prime_divisors()
Expand All @@ -2968,8 +2976,23 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
[2, 5]
sage: a = 2004; a.prime_divisors()
[2, 3, 167]
Setting the optional ``limit`` argument works as expected::
sage: a = 10^100 + 1
sage: a.prime_divisors()
[73, 137, 401, 1201, 1601, 1676321, 5964848081,
129694419029057750551385771184564274499075700947656757821537291527196801]
sage: a.prime_divisors(limit=10^3)
[73, 137, 401]
sage: a.prime_divisors(limit=10^7)
[73, 137, 401, 1201, 1601, 1676321]
"""
return [r[0] for r in self.factor()]
res = [r[0] for r in self.factor(*args, **kwds)]
limit = kwds.get('limit')
if limit is not None:
res = [r for r in res if r <= limit]
return res

prime_factors = prime_divisors

Expand Down

0 comments on commit bbd91d3

Please sign in to comment.