-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The goal of this ticket is to - package Kim Walish's `primecount` package (the version of `prime_pi` in Sage has bugs, see #24960) - provide an experimental interface to it in `arith/prime_pi.pyx` (that will be merged with the current `prime_pi` in Sage in #25009). Note that - `primecount`'s build system uses cmake, which needs an upgrade (see #25109) - the latest release of `primecount` should work on both big-endian and little-indian processors but has been tested successfully - linking on Mac OSX looks fishy but seems to work fine Tarball (to be renamed `primecount-4.3.tar.gz` in Sage): - https://github.com/kimwalisch/primecount/archive/v4.3.tar.gz This ticket led to opening and solving an upstream issue, leading to improvements in the build and documentation: - kimwalisch/primecount#11 URL: https://trac.sagemath.org/24966 Reported by: vdelecroix Ticket author(s): Vincent Delecroix Reviewer(s): Vincent Klein, François Bissey
- Loading branch information
Showing
10 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
= primecount = | ||
|
||
== Description == | ||
|
||
primecount is a C++ implementation of several algorithms for | ||
counting primes maintained by Kim Walisch. | ||
|
||
Website: https://github.com/kimwalisch/primecount/ | ||
|
||
== License == | ||
|
||
primecount is licensed BSD 2 | ||
|
||
== Upstream Contact == | ||
|
||
* https://github.com/kimwalisch/primecount/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tarball=primecount-VERSION.tar.gz | ||
sha1=c5e4912f75f096c2a3841c045c85207a78bcd6b5 | ||
md5=f06957db6ff92132584794aa53e5b2c8 | ||
cksum=1834548948 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cmake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
############################################################################### | ||
# | ||
# primecount Sage check script | ||
# | ||
############################################################################### | ||
|
||
if [ "$SAGE_LOCAL" = "" ]; then | ||
echo >&2 "Error: SAGE_LOCAL undefined - exiting..." | ||
echo >&2 "Maybe run 'sage -sh'?" | ||
exit 1 | ||
fi | ||
|
||
cd src | ||
$MAKE test | ||
|
||
if [ $? -ne 0 ]; then | ||
echo >&2 "Error: primecount failed to pass its test suite." | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
############################################################################### | ||
# | ||
# primecount Sage install script | ||
# | ||
############################################################################### | ||
|
||
if [ "$SAGE_LOCAL" = "" ]; then | ||
echo >&2 "Error: SAGE_LOCAL undefined - exiting..." | ||
echo >&2 "Maybe run 'sage -sh'?" | ||
exit 1 | ||
fi | ||
|
||
cd src | ||
|
||
if [ "$SAGE_FAT_BINARY" = yes ]; then | ||
EXTRA_OPTS="-DWITH_POPCNT=OFF" | ||
fi | ||
|
||
echo "Configuring primecount." | ||
cmake . -DCMAKE_INSTALL_PREFIX=${SAGE_LOCAL} \ | ||
-DCMAKE_VERBOSE_MAKEFILE=ON \ | ||
-DBUILD_STATIC_LIBS=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DBUILD_TESTS=ON \ | ||
${EXTRA_OPTS} | ||
|
||
sdh_make | ||
|
||
sdh_make_install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
optional |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
r""" | ||
Interface to the primecount library | ||
""" | ||
#***************************************************************************** | ||
# Copyright (C) 2018 Vincent Delecroix <20100.delecroix@gmail.com> | ||
# | ||
# Distributed under the terms of the GNU General Public License (GPL) | ||
# as published by the Free Software Foundation; either version 2 of | ||
# the License, or (at your option) any later version. | ||
# http://www.gnu.org/licenses/ | ||
#***************************************************************************** | ||
|
||
from libc.stdint cimport int64_t | ||
from libcpp.string cimport string as cppstring | ||
from cpython.int cimport PyInt_FromString | ||
|
||
from cysignals.signals cimport sig_on, sig_off | ||
|
||
cimport sage.libs.primecount as primecount | ||
|
||
cdef inline int _do_sig(int64_t n): | ||
"threshold for sig_on/sig_off" | ||
return n >> 26 | ||
|
||
cpdef int64_t prime_pi(int64_t n, method=None) except -1: | ||
r""" | ||
Return the number of prime numbers smaller or equal than ``n``. | ||
INPUT: | ||
- ``n`` - an integer | ||
- ``method`` - ``None`` or a string that determines the primecount | ||
function to be called | ||
- ``"deleglise_rivat"`` | ||
- ``"legendre"`` | ||
- ``"lehmer"`` | ||
- ``"lmo"`` | ||
- ``"meissel"`` | ||
- ``"primesieve"`` | ||
EXAMPLES:: | ||
sage: from sage.interfaces.primecount import prime_pi # optional - primecount | ||
sage: prime_pi(1000) # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "deleglise_rivat") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "legendre") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "lehmer") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "lmo") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "meissel") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "primesieve") # optional - primecount | ||
168 | ||
sage: prime_pi(1000, "youpi") # optional - primecount | ||
Traceback (most recent call last): | ||
... | ||
ValueError: unknown method 'youpi' | ||
""" | ||
cdef int64_t ans | ||
if method is None: | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "deleglise_rivat": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_deleglise_rivat(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "legendre": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_legendre(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "lehmer": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_lehmer(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "lmo": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_lmo(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "meissel": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_meissel(n) | ||
if _do_sig(n): sig_off() | ||
elif method == "primesieve": | ||
if _do_sig(n): sig_on() | ||
ans = primecount.pi_primesieve(n) | ||
if _do_sig(n): sig_off() | ||
else: | ||
raise ValueError("unknown method {!r}".format(method)) | ||
|
||
return ans | ||
|
||
cpdef prime_pi_128(n): | ||
r""" | ||
Return the number of prime number smaller than ``n``. | ||
EXAMPLES:: | ||
sage: from sage.interfaces.primecount import prime_pi_128 # optional - primecount | ||
sage: prime_pi_128(1000) # optional - primecount | ||
168 | ||
sage: nth_prime_128(2**65) # not tested | ||
? | ||
""" | ||
cdef cppstring s = str(n) | ||
cdef bytes ans | ||
sig_on() | ||
ans = primecount.pi(s) | ||
sig_off() | ||
return PyInt_FromString(ans, NULL, 10) | ||
|
||
cpdef int64_t nth_prime(int64_t n) except -1: | ||
r""" | ||
Return the ``n``-th prime integer. | ||
EXAMPLES:: | ||
sage: from sage.interfaces.primecount import nth_prime # optional - primecount | ||
sage: nth_prime(168) # optional - primecount | ||
997 | ||
""" | ||
if n <= 0: | ||
raise ValueError("n must be positive") | ||
|
||
cdef int64_t ans | ||
if _do_sig(n): sig_on() | ||
ans = primecount.nth_prime(n) | ||
if _do_sig(n): sig_off() | ||
return ans | ||
|
||
cpdef int64_t phi(int64_t x, int64_t a): | ||
r""" | ||
Return the number of integers smaller or equal than ``x`` by any of the | ||
first ``a`` primes. | ||
This is sometimes called a "partial sieve function" or "Legendre-sum". | ||
EXAMPLES:: | ||
sage: from sage.interfaces.primecount import phi # optional - primecount | ||
sage: phi(1000, 3) # optional - primecount | ||
266 | ||
sage: phi(2**30, 100) # optional - primecount | ||
95446716 | ||
""" | ||
return primecount.phi(x, a) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# distutils: libraries = primecount | ||
# distutils: language = c++ | ||
|
||
from libc.stdint cimport int64_t | ||
from libcpp.string cimport string as cppstring | ||
|
||
cdef extern from "primecount.hpp" namespace "primecount": | ||
int64_t pi(int64_t x) | ||
|
||
cppstring pi(const cppstring& x) | ||
|
||
int64_t pi_deleglise_rivat(int64_t x) | ||
int64_t pi_legendre(int64_t x) | ||
int64_t pi_lehmer(int64_t x) | ||
int64_t pi_lmo(int64_t x) | ||
int64_t pi_meissel(int64_t x) | ||
int64_t pi_primesieve(int64_t x) | ||
|
||
int64_t nth_prime(int64_t n) | ||
|
||
int64_t phi(int64_t x, int64_t a) | ||
|
||
void set_num_threads(int num_threads) | ||
int get_num_threads() | ||
|
||
cppstring get_max_x() | ||
cppstring get_max_x(double alpha) | ||
|
||
cppstring primecount_version() |