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
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.3.tar.gz (198.3 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.6m Windows x86-64

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

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.5m Windows x86-64

primesieve-1.4.3-cp35-cp35m-win32.whl (96.4 kB view details)

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.4m Windows x86-64

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

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 2.7m Windows x86-64

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

Uploaded CPython 2.7m Windows x86

File details

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

File metadata

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

File hashes

Hashes for primesieve-1.4.3.tar.gz
Algorithm Hash digest
SHA256 3514d4875b40433a525e5f38e9a0b5a27d81a8ca9a65471f30283fdd17590db6
MD5 f9726ef92209e556599790c361c647c2
BLAKE2b-256 fab0195b9afe71dd221918577635084c339ada2dce8ffb9ba1d5fdfafbaddaa6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 7bacf768b1aa778d817b54e14a06883ead6b4eca0cae144eb3ac8d25b35f05af
MD5 75d6aa24d8084d59a7aca578e9a74532
BLAKE2b-256 52729089f705c966fad7e09429523ac575bb6d8aeaf499b4cdbcefe635c8556d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 c18eecafb80cb718d8cc5dd658fd2b240f3310b979c656e36cb3773245433ccd
MD5 ee0babb186618548a8e24955eba54d44
BLAKE2b-256 6889f351c12dc1fb6284ee62bf0007431565e256fb756fbebca0442abd6ab00e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 9ce4fe6ba00e0d0e304b5f305de6436b5cb447dad914d1dce14bd20fde5abbdb
MD5 bbc2236a96a0f01db476d5b211c6d2ac
BLAKE2b-256 e178b94651dd498726904d83f50612f5b1284e7e69d54b4c89075dea1dd05068

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 967284000362a05376fbd55c4ac300d752013abdba0d3d0d46776c4ec74b55d0
MD5 0494d128a04e8a2623188b5805da6b23
BLAKE2b-256 c9fbb0396323b1eed1c892306e52d75df2e5f0547cb6514d2ff671537f7802b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 16369e5f03aca5337bc345463e17d5880c3f73fc5f0246bb3a88f640b5af32cf
MD5 d0e8403e3f9d7c61457260d5291e4d87
BLAKE2b-256 90097a959bc2a6ff349deefff658eaf0b2650ac2693a4f6e9eed7d3c7f16fae7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 9241c9d358464b079a08d5d92cd4fa6df5571071e3c56771364318e37acbed0e
MD5 5ac3f6244aa86f124711485b6a00c5e5
BLAKE2b-256 fa4a35c2ecc2e6b27ca91fa74be5093292605c025da88a7b21c417b567112d0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 f408c530965f86e0f9914ef100fb07ad59defc7b012b25b587ddc7fde0cd57bc
MD5 658e7eab83cbfdeeaa13f8426eeb3613
BLAKE2b-256 b18afcfa8e51ed6f43b41acb2209c3abfdf3ba657920e9292332221603d72f20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.3-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 38672ed9d1cc529fd494e790fdd5f979610b40c6267a54a3f8d8a258c22b5cfb
MD5 86f4818dd54c9ededa07682dea5aef84
BLAKE2b-256 3b37f8665bfe39ba63873a1f94a4ab6db9175640a2f48ac0d0e18cacb8774aeb

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