Skip to content

Commit 90b6f3d

Browse files
committedSep 19, 2021
2 parents 2273487 + 41df930 commit 90b6f3d

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed
 

‎src/sage/libs/pari/convert_sage.pxd

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ cpdef Gen new_gen_from_rational(Rational self)
1111

1212
cpdef pari_is_prime(Integer p)
1313
cpdef pari_is_prime_power(Integer q, bint get_data)
14+
cpdef unsigned long pari_maxprime()
15+
cpdef list pari_prime_range(long c_start, long c_stop, bint py_ints=*)

‎src/sage/libs/pari/convert_sage.pyx

+48-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ from cypari2.gen cimport objtogen
2626
from cypari2.stack cimport new_gen
2727
from .convert_gmp cimport INT_to_mpz, new_gen_from_mpz_t, new_gen_from_mpq_t, INTFRAC_to_mpq
2828

29-
from sage.libs.gmp.mpz cimport mpz_fits_slong_p, mpz_sgn, mpz_get_ui, mpz_set, mpz_set_si
29+
from sage.ext.stdsage cimport PY_NEW
30+
from sage.libs.gmp.mpz cimport mpz_fits_slong_p, mpz_sgn, mpz_get_ui, mpz_set, mpz_set_si, mpz_set_ui
3031
from sage.libs.gmp.mpq cimport mpq_denref, mpq_numref
3132
from sage.rings.integer cimport smallInteger
3233
from sage.rings.all import RealField, ComplexField, QuadraticField
@@ -527,3 +528,49 @@ cpdef pari_is_prime_power(Integer q, bint get_data):
527528
return (smallInteger(p), smallInteger(n)) if get_data else True
528529
else:
529530
return (q, smallInteger(0)) if get_data else False
531+
532+
533+
cpdef unsigned long pari_maxprime():
534+
"""
535+
Return to which limit PARI has computed the primes.
536+
537+
EXAMPLES::
538+
539+
sage: from sage.libs.pari.convert_sage import pari_maxprime
540+
sage: a = pari_maxprime()
541+
sage: res = prime_range(2, 2*a)
542+
sage: b = pari_maxprime()
543+
sage: b >= 2*a
544+
True
545+
"""
546+
return maxprime()
547+
548+
549+
cpdef list pari_prime_range(long c_start, long c_stop, bint py_ints=False):
550+
"""
551+
Return a list of all primes between ``start`` and ``stop - 1``, inclusive.
552+
553+
.. SEEALSO::
554+
555+
:func:`sage.rings.fast_arith.prime_range`
556+
557+
TESTS::
558+
559+
sage: from sage.libs.pari.convert_sage import pari_prime_range
560+
sage: pari_prime_range(2, 19)
561+
[2, 3, 5, 7, 11, 13, 17]
562+
"""
563+
cdef long p = 0
564+
cdef byteptr pari_prime_ptr = diffptr
565+
res = []
566+
while p < c_start:
567+
NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
568+
while p < c_stop:
569+
if py_ints:
570+
res.append(p)
571+
else:
572+
z = <Integer>PY_NEW(Integer)
573+
mpz_set_ui(z.value, p)
574+
res.append(z)
575+
NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
576+
return res

‎src/sage/rings/fast_arith.pyx

+14-32
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ Basic arithmetic with C integers
3636
# The int definitions
3737

3838
from libc.math cimport sqrt
39-
from sage.libs.gmp.mpz cimport mpz_set_ui
4039

41-
from sage.ext.stdsage cimport PY_NEW
42-
43-
from cypari2.paridecl cimport *
44-
from cypari2.gen cimport Gen as pari_gen
45-
from sage.libs.pari.all import pari
4640
from sage.rings.integer cimport Integer
4741

4842
cpdef prime_range(start, stop=None, algorithm=None, bint py_ints=False):
@@ -152,8 +146,6 @@ cpdef prime_range(start, stop=None, algorithm=None, bint py_ints=False):
152146
- Robert Bradshaw (speedup using Pari prime table, py_ints option)
153147
"""
154148
cdef Integer z
155-
cdef long c_start, c_stop, p
156-
cdef byteptr pari_prime_ptr
157149
# input to pari.init_primes cannot be greater than 436273290 (hardcoded bound)
158150
DEF init_primes_max = 436273290
159151
DEF small_prime_max = 436273009 # a prime < init_primes_max (preferably the largest)
@@ -187,42 +179,32 @@ cpdef prime_range(start, stop=None, algorithm=None, bint py_ints=False):
187179
algorithm = "pari_isprime"
188180

189181
if algorithm == "pari_primes":
182+
from sage.libs.pari.convert_sage import pari_maxprime, pari_prime_range
183+
from sage.libs.pari import pari
190184

191185
if max(start, stop or 0) > small_prime_max:
192186
raise ValueError('algorithm "pari_primes" is limited to primes larger than'
193187
+ ' {}'.format(small_prime_max - 1))
194188

195189
if stop is None:
196190
# In this case, "start" is really stop
197-
c_start = 1
198-
c_stop = start
191+
stop = start
192+
start = 1
199193
else:
200-
c_start = start
201-
c_stop = stop
202-
if c_start < 1:
203-
c_start = 1
204-
if c_stop <= c_start:
194+
start = start
195+
stop = stop
196+
if start < 1:
197+
start = 1
198+
if stop <= start:
205199
return []
206200

207-
if maxprime() < c_stop:
201+
if pari_maxprime() < stop:
208202
# Adding prime_gap_bound should be sufficient to guarantee an
209203
# additional prime, given that c_stop <= small_prime_max.
210-
pari.init_primes(min(c_stop + prime_gap_bound, init_primes_max))
211-
assert maxprime() >= c_stop
212-
213-
pari_prime_ptr = diffptr
214-
p = 0
215-
res = []
216-
while p < c_start:
217-
NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
218-
while p < c_stop:
219-
if py_ints:
220-
res.append(p)
221-
else:
222-
z = <Integer>PY_NEW(Integer)
223-
mpz_set_ui(z.value, p)
224-
res.append(z)
225-
NEXT_PRIME_VIADIFF(p, pari_prime_ptr)
204+
pari.init_primes(min(stop + prime_gap_bound, init_primes_max))
205+
assert pari_maxprime() >= stop
206+
207+
res = pari_prime_range(start, stop, py_ints)
226208

227209
elif (algorithm == "pari_isprime") or (algorithm == "pari_primes"):
228210
from sage.arith.all import primes

0 commit comments

Comments
 (0)