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
You need to have installed a C++ compiler on all OSes except Windows.
# Ubuntu/Debian
sudo apt install g++ python-dev
# Fedora
sudo dnf install gcc-c++ python-devel
# macOS
xcode-select --install
Installation
# Python 3 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/hickford/primesieve-python
cd primesieve-python
# Build and install primesieve-python
pip install . --upgrade
# Run tests
py.test
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.0.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73e7755ef8002229dbcab1ba259dcd00ed88483ba4ba028697f3491e07ad76b2 |
|
MD5 | cce9c51b70b1cb3e9116a948ad3cd920 |
|
BLAKE2b-256 | 6f9e401e5c727f99ee9513d47066758e2a3174be8ee7dd505b98e297cddfac69 |
Hashes for primesieve-2.0.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 080894c80ede1977689ade573e985c2b55d78c7e21a7fb5688b2a490e3b12073 |
|
MD5 | 317e4b4695f8a76186cc5a5f6a77eba9 |
|
BLAKE2b-256 | 989e4798b37796ab55e054c04a89ac0225c06b08c3980e082b2a8509b99ecf51 |
Hashes for primesieve-2.0.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6b136a2fbd78d69f3942da706a2c916d569aee31df75a5782fa941d06652e66 |
|
MD5 | 463cc0a4e23c3a937f5ceb673686a32a |
|
BLAKE2b-256 | fce38d3093fc9f5a20f0a592d556e9e6028fd5f9e64e84e2698bd2eb3817af78 |
Hashes for primesieve-2.0.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff9ed8e3e5796d39448ae38fd42df0852b8bc03481d694a1c407bc839b19fbb8 |
|
MD5 | e2eab5a38eacd2299e488823e598a563 |
|
BLAKE2b-256 | 49b717fc29889601f25f6d2622a4fbc59cd067e6f1e51e31b35ef1b295c25f0a |
Hashes for primesieve-2.0.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e32ae43775efaa200128cbecc2f38c95f4f62bd45a23ea746c76b2e77701c6ce |
|
MD5 | bdb0a03d44e31628ae953b34a6fa9009 |
|
BLAKE2b-256 | be2d9c9e91615e8048a78bddc1606c8a1fd36bf02f917ef535b8fc8aaa289b3d |
Hashes for primesieve-2.0.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 796b998d651c2df95d5cd8041803e060770101f7aaab4af27150adb0a9500b85 |
|
MD5 | 850c733d4b7340b04fdc1057b1f685b0 |
|
BLAKE2b-256 | 998e64b80dfde0921c0b2f1e62f9fe7d8303822ee66d024e2ed0d718a37ae3ef |
Hashes for primesieve-2.0.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb7af0d6cfe9843be850abac522c0244483c42e938e18c645b31533fb8d5033c |
|
MD5 | 6bd155a0f039c6db84486deb0162eadc |
|
BLAKE2b-256 | 7094584160e77fde9b01e1d29fe3318cec6468f5f1a0b3b69bc9db10f16b3ba3 |
Hashes for primesieve-2.0.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 473287c7ba9a0ce0d71b318a2d81cf842d9b68ace4051a61c81462f4d1ec3458 |
|
MD5 | 9cb9abc14b8738b494698bb15d9d8a21 |
|
BLAKE2b-256 | 2195ad75339cac1673131c82fed9a3ea7ff450a36fa64c4d026d8a76461f9498 |