Skip to content

Commit

Permalink
trac 33360: avoid factoring in is_prime()
Browse files Browse the repository at this point in the history
In the added TEST, the ideal norm is product of two primes but factoring
this product takes about half an hour, so factoring the ideal is slow.

To fix the issue, we use PARI's idealismaximal() function instead.

Co-authored-by: Gonzalo Tornaría <tornaria@cmat.edu.uy>
Co-authored-by: Lorenz Panny <lorenz@yx7.cc>
  • Loading branch information
tornaria and yyyyx4 committed Feb 23, 2023
1 parent 05329f6 commit 3d4d764
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/sage/rings/number_field/number_field_ideal.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,16 +996,38 @@ def is_prime(self):
False
sage: K.ideal(17).is_prime() # ramified
False
TESTS:
Check that we do not factor the norm of the ideal, this used
to take half an hour, see :trac:`33360`::
sage: K.<a,b,c> = NumberField([x^2-2,x^2-3,x^2-5])
sage: t = (((-2611940*c + 1925290/7653)*b - 1537130/7653*c
....: + 10130950)*a + (1343014/7653*c - 8349770)*b
....: + 6477058*c - 2801449990/4002519)
sage: t.is_prime()
False
"""
try:
return self._pari_prime is not None
except AttributeError:
F = self.factor() # factorization with caching
if len(F) != 1 or F[0][1] != 1:
self._pari_prime = None
else:
self._pari_prime = F[0][0]._pari_prime
return self._pari_prime is not None
pass

K = self.number_field().pari_nf()
I = self.pari_hnf()

candidate = K.idealismaximal(I) or None

# PARI uses probabilistic primality testing inside idealismaximal().
if get_flag(None, 'arithmetic'):
# proof required, check using isprime()
if candidate and not candidate[0].isprime():
candidate = None

self._pari_prime = candidate

return self._pari_prime is not None

def pari_prime(self):
r"""
Expand Down

0 comments on commit 3d4d764

Please sign in to comment.