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.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74728d9c5c3aa336de369f00a17d44966f1d7a6589019754ebd0589c3d568718 |
|
MD5 | b504cddf40d5ee22b2cb8616c1e96426 |
|
BLAKE2b-256 | 8d7f0985c61d1f13810821f5168de048c99e6cfb63272658b0b6ab7e7a3da955 |
Hashes for primesieve-2.1.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f04576a4e49d2cdbb42f94ae7991dbc048dd66eb2c67a2ec6bd842e576e27faf |
|
MD5 | a97f5448b70eea8020bb4c186cb14a7b |
|
BLAKE2b-256 | a622d2169a4e47066190223526539f2537b599c77c49d637e81d0ab9240bb8d7 |
Hashes for primesieve-2.1.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d637142bc6f86530add52ac9e97ebe96c52fa6dbe15ec31a355f91cde499e164 |
|
MD5 | 9ab89b7040657eeb9e473f35a65bdc6b |
|
BLAKE2b-256 | d14470f81ff48fe295456051c480d287369f81c93c19682421a568c50de6cf28 |
Hashes for primesieve-2.1.0-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83c53b40bab7319544ea42138962451c1076f3f51c10c344a4ded73487d5ae55 |
|
MD5 | c978462f2df7ca7e1826fac362f6cfe8 |
|
BLAKE2b-256 | a24b1d8c113f464e23e00500a32bf79fd4c9fa36525bd580804b1460716a3e82 |
Hashes for primesieve-2.1.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c774715550afba79927714c19ea8c2890d3d60f1a749d66a0fff5cd0dd2529f9 |
|
MD5 | 35fbbd926bbd99a3e81086836050d585 |
|
BLAKE2b-256 | 9a04b482761702784d7710213867f631b3d6287aa85dda04ec6844f5fd0891eb |
Hashes for primesieve-2.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38129139dff22be46a2bb95489606abb0cd85bb087cd8259d9a522320b631c9e |
|
MD5 | b8e45dc2317f3b89cb67be52f6619064 |
|
BLAKE2b-256 | 49d64b0e67689624b739515e11e95c00eb78119a80efd217df1cea5de2d903bb |
Hashes for primesieve-2.1.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ddba4c7c39e6cc1a9e5956eb1b2dae02e7e131dbe7183799c2ed08b7e1bbf4b |
|
MD5 | 8043ac66b3a12372845a4af0f1d3a12b |
|
BLAKE2b-256 | 9a7d52b9d26309811387a49e76a110528fcc50db45986e515149a0fa55c2e676 |
Hashes for primesieve-2.1.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37813c9919d445fe66af16b8728b174e0bc178db90e298ba08af850e1a74bc89 |
|
MD5 | ab9a1a963a7502bb9aab1e66bf3d7a08 |
|
BLAKE2b-256 | bfa54255df6e4eca2581b6e8c8386dc22d9563759f310551124c7f40a13b0595 |
Hashes for primesieve-2.1.0-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b25a9c47cce807f28495ee4d238eeb5842b75541209bab908ea7635fa0af2a6c |
|
MD5 | 7e0dda0a29cb87603176720edc13563d |
|
BLAKE2b-256 | 0d0c226595ea1ce484b9cd24834383863108dc562b215d87cd8af74de687ab19 |
Hashes for primesieve-2.1.0-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83e124976d20c4862c673699abc06dc5772a3cde26c5f70f7b3e9c4423ab4ebc |
|
MD5 | 2821cb03b016d1b16f19e29317127495 |
|
BLAKE2b-256 | 9942a64d491bcb8e6df5f6c0c52df1ad9133f5603f96997353f9b3a68ab2c0dc |
Hashes for primesieve-2.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dacd199cfde7c46483a300e7a3b8871575ada3f1c5ce52d77f0efeff9e52e579 |
|
MD5 | 57c6e2d9233cbdf3a85c2fbd5bf19436 |
|
BLAKE2b-256 | 03f3fdcc8c65fba5b11c095d855fb1f5752186f7967ddfdd7a099e5bad9553ef |
Hashes for primesieve-2.1.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fa2ce2a5f404b1d53c77635248128410dba4070a7dce18480eabce230d995ec |
|
MD5 | 4e018ea1fecb212fc85788ce7950841c |
|
BLAKE2b-256 | 9797ed77db138c16d16035b2fe21520f3ccd2576d6070a5ae3d4d5f7ec1998eb |
Hashes for primesieve-2.1.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b822dd90bcc2d5d205607907801afd088ec6878e5280d896af7d15d5bf5229d |
|
MD5 | 3e6def8c211961424aea683137adf47c |
|
BLAKE2b-256 | cd17be90b3f24257488f683b693ac45c057959d4347d223206affcbc7b3e9ec1 |
Hashes for primesieve-2.1.0-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5225e430e631b215f6acf7a04c6faa51fdd492006d48868c94dd1ea62fc4bc77 |
|
MD5 | 8e7be305f1f2c185b4aac52c542151d6 |
|
BLAKE2b-256 | 56285d46e3b0ad212a57834d8acebb4810422b2ef23e7559f1e1d6fadc1b33e7 |
Hashes for primesieve-2.1.0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71347a585a79d29ed7a18d121bbead7ff25f8391a1536bea01e1193d64c7d0c3 |
|
MD5 | 27df61895f57c90744d45fde2cafd239 |
|
BLAKE2b-256 | b79a37868a27c578ffc98d4edd574e646bbefea3ce1b3b9223839ec4bde9e1fb |
Hashes for primesieve-2.1.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 941487b61b66766a4c5ba0f372aaf1df06da5f451f51e54f8224046d743d82a6 |
|
MD5 | b962aa4cd12920a9176acbc300c7793f |
|
BLAKE2b-256 | 0b2d9aacb842082b20735eabbf6bf96db1c4c1b5a93fdb5dbd37befd807e9451 |
Hashes for primesieve-2.1.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4b3d370d01150812816e390fe219f7b4ad73f7365c9508bb450a15623e60294 |
|
MD5 | 36d8a6a1aa11e35b587fa39d20c1b57b |
|
BLAKE2b-256 | cc076bb4c69d1d00edb5d59e91d9859fb026b03b2b593d8affb2f8bb4b1ecf77 |
Hashes for primesieve-2.1.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2b4278f9a738a7d06b0569239f299176fa47d04c1cb2011de1a0a853b8f4b60 |
|
MD5 | b91c4df9e66ae39f2527a1e0b32f4128 |
|
BLAKE2b-256 | 7e8a2acb95855443a3e9e68ff4323de40cdc6654404072e1a9a3a3c2b8b0b8e3 |
Hashes for primesieve-2.1.0-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44475afbbf2927ddfdce99d80f7215dc4d60364455b54448bbcadf80a3e9d114 |
|
MD5 | ebd2b555c8005dd1401eb7febf515b9d |
|
BLAKE2b-256 | 3eacf45cec6840e15f8b447f7f52fdad378df1e8016d6b7c2ca7e21be7f97970 |
Hashes for primesieve-2.1.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 981cb7178c625d2ece79a20a32f507af948e9fecf3141b594135b6047187b05f |
|
MD5 | f46c62efeefa87f1ef2746102c44b234 |
|
BLAKE2b-256 | fda09245fbeadf16fdc5a65bdbbd82f1bdc134a3e9acb011d08c005e63e57f5a |