No project description provided
Project description
primesieve-python
Summary
Python bindings for the primesieve C++ library.
Generates primes orders of magnitude faster than any pure Python code!
Features:
- Get an array of primes
- Iterate over primes using little memory
- Find the nth prime
- Count/print primes and prime k-tuplets
- Multi-threaded for counting primes and finding the nth prime
- NumPy support
Prerequisites
We provide primesieve wheels (distribution packages) for Windows, macOS and Linux for x86 and x64 CPUs. For other operating systems and/or CPUs you need to have installed a C++ compiler.
# Ubuntu/Debian
sudo apt install g++ python-dev
# Fedora
sudo dnf install gcc-c++ python-devel
# macOS
xcode-select --install
Installation
# Python 3.5 or later
pip install primesieve
# For Python 2.7 use:
pip install "primesieve<=1.4.4"
Conda Installation
You don't need to install a C++ compiler when installing python-primesieve using Conda.
conda install -c conda-forge python-primesieve
Usage examples
>>> from primesieve import *
# Get an array of the primes <= 40
>>> primes(40)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
# Get an array of the primes between 100 and 120
>>> primes(100, 120)
[101, 103, 107, 109, 113]
# Get an array of the first 10 primes
>>> n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
# Get an array of the first 10 primes >= 1000
>>> n_primes(10, 1000)
[1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061]
# Get the 10th prime
>>> nth_prime(10)
29
# Count the primes below 10**9
>>> count_primes(10**9)
50847534
Here is a list of all available functions.
Iterating over primes
Instead of generating a large array of primes and then do something with the primes it is also possible to simply iterate over the primes which uses less memory.
>>> import primesieve
it = primesieve.Iterator()
prime = it.next_prime()
# Iterate over the primes below 10000
while prime < 10000:
print prime
prime = it.next_prime()
# Set iterator start number to 100
it.skipto(100)
prime = it.prev_prime()
# Iterate backwards over the primes below 100
while prime > 0:
print prime
prime = it.prev_prime()
NumPy support
Using the primesieve.numpy
module you can generate an array of
primes using native C++ performance!
In comparison the primesieve
module generates an array of primes
about 3 times slower mostly because the conversion of the C primes
array into a python array is quite slow.
>>> from primesieve.numpy import *
# Generate a numpy array with the primes below 100
>>> primes(100)
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97])
# Generate a numpy array with the first 100 primes
>>> n_primes(100)
array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
479, 487, 491, 499, 503, 509, 521, 523, 541])
Development
You need to have installed a C++ compiler, see Prerequisites.
# Install prerequisites
pip install cython pytest numpy
# Clone repository
git clone --recursive https://github.com/kimwalisch/primesieve-python
cd primesieve-python
# Build and install primesieve-python
pip install . --upgrade
# Run tests
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for primesieve-2.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bffedb3e3b3e0156eba88b5bf8ac251eba74c454ddbadaa3fe6d45c01d933f2c |
|
MD5 | c0f3c672a4c4e9e6ca9d3d1c6a53753b |
|
BLAKE2b-256 | a3e1a29cc004627ff0878dc3961b5f184ecc83e22f20f17410b9bab1fafd69fe |
Hashes for primesieve-2.2.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5deceded524853e9973948594ba42e284e95fe156e64568a8c8dab12238ba4a |
|
MD5 | 359a3d9b019bc528ccf2f900f3012b27 |
|
BLAKE2b-256 | 4a5c0fc2866ca2f69229d25d99d0b6ad18d952cc6168c4e7a46fc786b4b75a56 |
Hashes for primesieve-2.2.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bcf8dfde9520ee1b87cd707c3c664cab0b35c602023b2d5d7fa88d2008ac266 |
|
MD5 | 2d9eee70f17fb67616d016443658577a |
|
BLAKE2b-256 | 1a5dc6755a73505f38e0de0fed5e0fe5a89cfbdad3ef9d71021bac6d123719f9 |
Hashes for primesieve-2.2.0-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74a7bfc94281cbc6ff53259f43b603f71b70d868cfba1f14b2fdf57d83f9034a |
|
MD5 | 1217dffacecfc01e3c89cfd4199251cc |
|
BLAKE2b-256 | 042fc549fd40ab7472faa7563406448ecc4e5c3edc50cbbff8747a9f6877ec01 |
Hashes for primesieve-2.2.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece3ac379057c12b2ec888be04cc08f1df675e8af39cbe879db5415a7a1d2a8d |
|
MD5 | d181f1580e1d40fcbd6dae048b886cb5 |
|
BLAKE2b-256 | f335d6c14b26517fd4381d37371dd1c964d46bae509e0032a4bca83261399993 |
Hashes for primesieve-2.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79bdf4c188ae5a082a198d162d4de1b0cbc61695fe1016e1fb9ded5809068b2c |
|
MD5 | ad4664d9c3a4fe5a241f2abc0d6041a2 |
|
BLAKE2b-256 | 983bd9a09d197f7e07a4157891eb7b4f4e381d8a93ac59085776b7dbeaf138b5 |
Hashes for primesieve-2.2.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e021fea6de02d09034da0deff5ba73e695030efc498d6ea1ce8cfbcb93b3e36 |
|
MD5 | eb894c852920725d9c1a6dde04671657 |
|
BLAKE2b-256 | 7917089d1baba0c659f8370af88d2d28239bced0eb760aec2c183e5904b11c9e |
Hashes for primesieve-2.2.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cf35412c160f75883eb2081bcee952a9d23c171f82ef86540d349ac4bd73641 |
|
MD5 | bb935d38829530fc5be169deab8ce8c7 |
|
BLAKE2b-256 | 6ff1f800cc3b9ccee03722581d99ce6ad1e447fc4dd4dd687749c1bd79f65b8e |
Hashes for primesieve-2.2.0-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c54efea906de1b7262ad86cb55030df5bbd72910653b891cc3876514b64c3ad |
|
MD5 | 27b16b04cabb3370a532bf7655c57ce4 |
|
BLAKE2b-256 | dce5a43611c19a95f37b35cf1161ed799edc75f5af8859c2a3dedc7b31dd4df5 |
Hashes for primesieve-2.2.0-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1c381a36124bfc6908d628d993809fa81f5250967430d743e0381719780f5f0 |
|
MD5 | 395f6b939a7889ea9df252adb40e922d |
|
BLAKE2b-256 | 7c10e58780fbb4c3ca87bcf7ef9dbcf4171a08ceb9495fd891529105ad7be9db |
Hashes for primesieve-2.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d60002858168261b13a06ea3e772e6ada4be65ad03cfb49826c352bb84e0fadb |
|
MD5 | ecfa46b189de66b837acbeabe2eed541 |
|
BLAKE2b-256 | 626c579c509bf4361a4fb258768074384f9a5923339f7c300edd66758c420fd1 |
Hashes for primesieve-2.2.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ae918c0294260cb1bbe055288389d9bd517699ae325b54cfffd2848062cc749 |
|
MD5 | c67850bf227d81f3d6a97caf66afc152 |
|
BLAKE2b-256 | af57a3a8b7bb71f74900ba18a0dd77b4da1cb8c815dbbe00cf861663844c41b0 |
Hashes for primesieve-2.2.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ff88eaf4f9dd56583101bbbc1245b504b10ba73ab0c81c37fec149c4f299ced |
|
MD5 | dee1a9e021ff9838625350dff214abaa |
|
BLAKE2b-256 | 0a82ebc3d212b03752f50702aef4421c084a1ff59c1e706eb545f4bcd7afebbf |
Hashes for primesieve-2.2.0-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84a7fa4463b25ecb114c8928c44966481424e15c19484db66a69d94cec5f79ea |
|
MD5 | a6030e1e3b2886a2ba2ba2e1a5092213 |
|
BLAKE2b-256 | 2ac0180d2ada53c9210ffaf5bb6c29875e7cb8db0c0433fa7d9c4217411f92c4 |
Hashes for primesieve-2.2.0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ed105b0544459d2ee007c808a58f1e3042035ad84f8ea08f673a3c5d175ffd5 |
|
MD5 | 4ee758d9eeef061717c7541d8078fadb |
|
BLAKE2b-256 | fdf645591c042fe22282e89826594a83f18ee4e7c1d191218bd7c3ba5e5caa56 |
Hashes for primesieve-2.2.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f98546ac19699576dd51f7c2266fa1dc03db9ac886d04936bad701abbe9e888 |
|
MD5 | 4f4611af5dad4a4a9eca907737f9751d |
|
BLAKE2b-256 | 11a9593759ea20fefb67ac46b622727f6ad519ed373e22125324c1c4ac0cbc88 |
Hashes for primesieve-2.2.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f5b177259179f7da392ab908a84546df0c67f6d4f9d6ea560c3a470ad57bcc0 |
|
MD5 | eac8f50962fe9ad2729812d6696147f7 |
|
BLAKE2b-256 | ad87a2a4c8f3ab24ea4cc46af1780eb2c67f25c442a7fe097b7beecf0ce5286a |
Hashes for primesieve-2.2.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5566ca80473544f3b5796bd0f3c8a54f512a11fb68881775d763552a8988f347 |
|
MD5 | a309f8c97a289fd23ff019cc2fd1b5e8 |
|
BLAKE2b-256 | 0a953d6cdb125d22c3b7b24618f4cbd8b3f7892892b583c27e03a4e8304220c4 |
Hashes for primesieve-2.2.0-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79c497d91d479d9079ba155bf35d39342dcca77d8318d5f5724d2d548a600700 |
|
MD5 | 4feb1319da7ad862b0c68f60453eeb57 |
|
BLAKE2b-256 | 9f37f200724b0bd8691508b4be2c16afb655dc52e6ce0fc865f50bf0a4613378 |
Hashes for primesieve-2.2.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da40525778fe6db3a0a1ffa66ec7b28e469f49ae1ec40deced3c2827427c31bf |
|
MD5 | a0613f30a34a431a4c0586ba72a9617e |
|
BLAKE2b-256 | 45728d1986904ddb22415143c677f5ad66a2a8538e5a03ffa3b9ac03bc0dfbcf |