Skip to content

Commit

Permalink
trac #4939 -- massive performance regression to primes_first_n
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Jan 4, 2009
1 parent 57af87a commit 50082cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
22 changes: 9 additions & 13 deletions src/sage/rings/arith.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,8 @@ def primes_first_n(n, leave_pari=False):
Return the first $n$ primes.
INPUT:
leave_pari -- bool (default: False) if True the returned list
is a PARI list; this is *vastly* (10 times!)
faster since the time of prime_range is dominated
by conversion from PARI to SAGE integers.
However, PARI integers are much different than
SAGE integers. If you use this option the lower
bound must be 2.
n -- a nonnegative integer
OUTPUT:
a list of the first $n$ prime numbers.
Expand All @@ -538,13 +533,14 @@ def primes_first_n(n, leave_pari=False):
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
sage: len(primes_first_n(1000))
1000
This is very fast, because we leave the output as a PARI object:
sage: v = primes_first_n(10^6, leave_pari=True)
sage: len(v)
1000000
sage: primes_first_n(0)
[]
"""
return fast_arith.prime_range(pari.nth_prime(n)+1, leave_pari=leave_pari)
if n < 0:
raise ValueError, "n must be nonnegative"
if n < 1:
return []
return fast_arith.prime_range(pari.nth_prime(n) + 1)

#
# This is from
Expand Down
17 changes: 1 addition & 16 deletions src/sage/rings/fast_arith.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ from sage.rings.integer cimport Integer
cdef extern from "convert.h":
cdef void t_INT_to_ZZ(mpz_t value, long *g)

cpdef prime_range(start, stop=None, leave_pari=False):
cpdef prime_range(start, stop=None):
r"""
List of all primes between start and stop-1, inclusive. If the
second argument is omitted, returns the primes up to the first
Expand All @@ -66,8 +66,6 @@ cpdef prime_range(start, stop=None, leave_pari=False):
INPUT:
start -- lower bound
stop -- upper bound
leave_pari -- (default: False) if True, return a list of Pari
integers instead of Sage integers.
EXAMPLES:
sage: prime_range(10)
Expand All @@ -88,16 +86,6 @@ cpdef prime_range(start, stop=None, leave_pari=False):
TESTS:
sage: len(prime_range(25000,2500000))
180310
sage: prime_range(8,leave_pari=True)
[2, 3, 5, 7]
sage: type(prime_range(7,leave_pari=True)[0])
<type 'sage.libs.pari.gen.gen'>
sage: prime_range(2,2,leave_pari=True)
[]
sage: prime_range(2,3,leave_pari=True)
[2]
sage: prime_range(5,10,leave_pari=True)
[5, 7]
AUTHORS:
- William Stein (original version)
Expand All @@ -118,9 +106,6 @@ cpdef prime_range(start, stop=None, leave_pari=False):
m = int(ZZ(pari(start-1).primepi()))
n = lg(v.g) - 1

if leave_pari:
return v[m:]

res = [0] * (n-m)
for ind from m <= ind < n:
tmp = PY_NEW(Integer)
Expand Down

0 comments on commit 50082cd

Please sign in to comment.