From 3d4d764e1924f9fdb6dfc36a97dc1f587a34011c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Tornar=C3=ADa?= Date: Mon, 28 Feb 2022 23:15:16 -0300 Subject: [PATCH] trac 33360: avoid factoring in is_prime() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Lorenz Panny --- .../rings/number_field/number_field_ideal.py | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/sage/rings/number_field/number_field_ideal.py b/src/sage/rings/number_field/number_field_ideal.py index d5f7157217f..8ca1f958039 100644 --- a/src/sage/rings/number_field/number_field_ideal.py +++ b/src/sage/rings/number_field/number_field_ideal.py @@ -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. = 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"""