Skip to main content

Fast prime number generator. Python bindings for primesieve C/C++ library

Project description

primesieve-python

Build Status Build status PyPI GitHub license

Python bindings for the primesieve C++ library.

Generates primes orders of magnitude faster than any pure Python code!

Features:

  • Get a list 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: first install Xcode using App Store
xcode-select --install

Installation

pip install primesieve

Usage examples

>>> from primesieve import *

# Get a list of the primes <= 40
>>>  primes(40)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

# Get a list of the primes between 100 and 120
>>>  primes(100, 120)
[101, 103, 107, 109, 113]

# Get a list of the first 10 primes
>>>  n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

# Get a list 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 list 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 a list of primes about 7 times slower mostly because the conversion of the C++ primes array into a python list is very 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

primesieve-1.4.1.tar.gz (198.4 kB view details)

Uploaded Source

Built Distributions

primesieve-1.4.1-cp36-cp36m-win_amd64.whl (106.3 kB view details)

Uploaded CPython 3.6m Windows x86-64

primesieve-1.4.1-cp36-cp36m-win32.whl (96.6 kB view details)

Uploaded CPython 3.6m Windows x86

primesieve-1.4.1-cp35-cp35m-win_amd64.whl (106.1 kB view details)

Uploaded CPython 3.5m Windows x86-64

primesieve-1.4.1-cp35-cp35m-win32.whl (96.5 kB view details)

Uploaded CPython 3.5m Windows x86

primesieve-1.4.1-cp34-cp34m-win_amd64.whl (115.8 kB view details)

Uploaded CPython 3.4m Windows x86-64

primesieve-1.4.1-cp34-cp34m-win32.whl (108.2 kB view details)

Uploaded CPython 3.4m Windows x86

primesieve-1.4.1-cp27-cp27m-win_amd64.whl (117.6 kB view details)

Uploaded CPython 2.7m Windows x86-64

primesieve-1.4.1-cp27-cp27m-win32.whl (106.3 kB view details)

Uploaded CPython 2.7m Windows x86

File details

Details for the file primesieve-1.4.1.tar.gz.

File metadata

  • Download URL: primesieve-1.4.1.tar.gz
  • Upload date:
  • Size: 198.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for primesieve-1.4.1.tar.gz
Algorithm Hash digest
SHA256 f56856779ff3f8fc02997ae0db50430e362fead25d8d39f7a4ae0627e6ce3052
MD5 4f631f57b4c9700b70e2c024dd5c452a
BLAKE2b-256 f4a3cbb7330b70d190ddd04d5f60a4b1bc75a177af3069100e76f6bde0fcc564

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 be9257d94db4bba4ca1aa5f17d85d301e9875b42eeed4c3f00b9e677253fce50
MD5 c7abd8bd502e5e13398734f46d7aff7f
BLAKE2b-256 36f8033996ab0a28e728923331b0c5244f720af2c77aedcf1fe3acd6d46097b3

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 e57d83716970d6d963433d216c3287cd6b1cee051508bd2c5d7ac12f1c846870
MD5 b65274343a58e602ba5e19b50990737f
BLAKE2b-256 96f0542bb55aa462dea1a3e90103c6d78405ff13130bb964b77f87f3e667c160

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 bba3a970250e5c83a1d9b6822770eb54b2a4da0f1ebed0de849a46c63a1a63e8
MD5 21535c73e61c49f9f31dfc26efd3f4c4
BLAKE2b-256 902527d9a651168f36a65f3b01d0b49719ba754b7c82ff1be5ebd714333c494a

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 aa020817260f1388029cd47c90804d0a3bdd3f3af2571df47eb771e24bf50f8a
MD5 5a7927faec00c4180d7d345db04f0fcf
BLAKE2b-256 05bc9251bad2265d2488026974952afea1a516b9e73c6347685656520df4c66a

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 11c005d21430723c2f0ae4281c6181e55c5e96d52e84e9d62e8c641b53958506
MD5 f70057295a8917103d76cc1491a292fa
BLAKE2b-256 e794537c9fbf3b3e04fba59a2f84050d0b791b79b1d208bec8dee8c4a3bf66cf

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 002c2bb4d06ba3096c5274babaf4257b950fa3d64831cf6f9e406d6c17679aec
MD5 eed9910d705e7f3412cc99d6984a13a1
BLAKE2b-256 ca9260681c602c7a67479b305971f7fb44b48f26fdd7445ced200012f4112b0f

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 dee13da1dcb78c95c1fa9b5dce707b66d801ab5256e0d81fbefe2826ce9ddaf3
MD5 280f601490b8d62c1b41bf0bf197d68e
BLAKE2b-256 d8e9f45da04176acaf8c0840564d03ac35ead4db6d53e59f9ab2cd6b96d515f0

See more details on using hashes here.

File details

Details for the file primesieve-1.4.1-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for primesieve-1.4.1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 8c149807b8cf29014a0a4c9e0f17ed4fe6bfa0f4f97b83f85ca601400ba05e92
MD5 70b85cdd2f6be56b57f9d279965a6a2f
BLAKE2b-256 e69d9deaf0baea99f36d34d3a220334d846512f35db4d609ff400734c4bc1692

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page