Skip to content

Commit 3d4d764

Browse files
tornariayyyyx4
andcommitted
trac 33360: avoid factoring in is_prime()
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 <[email protected]> Co-authored-by: Lorenz Panny <[email protected]>
1 parent 05329f6 commit 3d4d764

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/sage/rings/number_field/number_field_ideal.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -996,16 +996,38 @@ def is_prime(self):
996996
False
997997
sage: K.ideal(17).is_prime() # ramified
998998
False
999+
1000+
TESTS:
1001+
1002+
Check that we do not factor the norm of the ideal, this used
1003+
to take half an hour, see :trac:`33360`::
1004+
1005+
sage: K.<a,b,c> = NumberField([x^2-2,x^2-3,x^2-5])
1006+
sage: t = (((-2611940*c + 1925290/7653)*b - 1537130/7653*c
1007+
....: + 10130950)*a + (1343014/7653*c - 8349770)*b
1008+
....: + 6477058*c - 2801449990/4002519)
1009+
sage: t.is_prime()
1010+
False
9991011
"""
10001012
try:
10011013
return self._pari_prime is not None
10021014
except AttributeError:
1003-
F = self.factor() # factorization with caching
1004-
if len(F) != 1 or F[0][1] != 1:
1005-
self._pari_prime = None
1006-
else:
1007-
self._pari_prime = F[0][0]._pari_prime
1008-
return self._pari_prime is not None
1015+
pass
1016+
1017+
K = self.number_field().pari_nf()
1018+
I = self.pari_hnf()
1019+
1020+
candidate = K.idealismaximal(I) or None
1021+
1022+
# PARI uses probabilistic primality testing inside idealismaximal().
1023+
if get_flag(None, 'arithmetic'):
1024+
# proof required, check using isprime()
1025+
if candidate and not candidate[0].isprime():
1026+
candidate = None
1027+
1028+
self._pari_prime = candidate
1029+
1030+
return self._pari_prime is not None
10091031

10101032
def pari_prime(self):
10111033
r"""

0 commit comments

Comments
 (0)