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

Uploaded Source

Built Distributions

primesieve-1.4.4-cp37-cp37m-win_amd64.whl (106.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

primesieve-1.4.4-cp37-cp37m-win32.whl (96.5 kB view details)

Uploaded CPython 3.7m Windows x86

primesieve-1.4.4-cp36-cp36m-win_amd64.whl (106.6 kB view details)

Uploaded CPython 3.6m Windows x86-64

primesieve-1.4.4-cp36-cp36m-win32.whl (96.7 kB view details)

Uploaded CPython 3.6m Windows x86

primesieve-1.4.4-cp35-cp35m-win_amd64.whl (106.3 kB view details)

Uploaded CPython 3.5m Windows x86-64

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

Uploaded CPython 3.5m Windows x86

primesieve-1.4.4-cp34-cp34m-win_amd64.whl (116.0 kB view details)

Uploaded CPython 3.4m Windows x86-64

primesieve-1.4.4-cp34-cp34m-win32.whl (108.4 kB view details)

Uploaded CPython 3.4m Windows x86

primesieve-1.4.4-cp27-cp27m-win_amd64.whl (117.7 kB view details)

Uploaded CPython 2.7m Windows x86-64

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

Uploaded CPython 2.7m Windows x86

File details

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

File metadata

  • Download URL: primesieve-1.4.4.tar.gz
  • Upload date:
  • Size: 251.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4.tar.gz
Algorithm Hash digest
SHA256 58bbff5c6928254e4fadd21ce324d8bae97aa706f72d69b1b0e1bf7ed6a1925c
MD5 307c949fa34bc1d0725e04f972463a51
BLAKE2b-256 434fcd9b85d154bc94eec5d4a8cfdbbf96d0e487cecd46495f86b9d5d481d18d

See more details on using hashes here.

File details

Details for the file primesieve-1.4.4-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: primesieve-1.4.4-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 106.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5044ac59098d59380b735c8009d28f095e8a15debe31f6cd5371fde7cbd96074
MD5 ab53b19c394c50546dcc16c0792be901
BLAKE2b-256 b4b94dbdacbe57c310dc5093977697e3fac27f76b9ffca375373df7783f5841c

See more details on using hashes here.

File details

Details for the file primesieve-1.4.4-cp37-cp37m-win32.whl.

File metadata

  • Download URL: primesieve-1.4.4-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 96.5 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 9c83bc0e5b912b334ba73cfa236e98f7adb9e36dba2ed98e7eef7bacad82f1f2
MD5 6db99eae88c66290b4b939e59d51e59e
BLAKE2b-256 acbbe11a37cc69e9128f8a22955676040ea6fefe2dc4d737c99ffb418524d9d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 106.6 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 aae0f58ed9f84c57960ceec5c22631ba89768df2cfa88c754ad4b46da3d08d63
MD5 16d8dbab3c408b38163015dbb8d40372
BLAKE2b-256 8b2e268defaf7e9e02e30360be4442918027d6fe13ad1297e202e01bd100e8d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 96.7 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 465e0851762c7149e2d656928fc50762bf0bfb8cdca865b65374c70044cf5328
MD5 9a97089a560e7122c4bac066d0b0cccb
BLAKE2b-256 e4ff3650cd5027c05cdb4279802976fac7b075c7bf25b6a3b5a75c5e3686c164

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 106.3 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 77625ebb1547fc2fab18711a0545104b1d0e1f29cb1d30149ab36c1797e4f048
MD5 bedeb1413ceceb858a455be1bc16a4ce
BLAKE2b-256 278111e28cd43091da9691a5224a4b0aff153ff1d89082bdf708da2d46602ebd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 96.5 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 2a399269c44098ee2b17f86f3c689a659e887f5f69083229f68104934c0a25ab
MD5 305e102cd8389544c0cc163ebc69a065
BLAKE2b-256 52d801698a0fb26f58e18478aa77a9b5e6bb91db037429534774227fd1b85c16

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp34-cp34m-win_amd64.whl
  • Upload date:
  • Size: 116.0 kB
  • Tags: CPython 3.4m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 22de838c05ae7cbe91f86d6169a3edad4cc40508f4ba8ff9345f9448e01ba2b2
MD5 d48d9bed1ec8081fce962c7841b7819d
BLAKE2b-256 ead508402fce2194a6a8732272cfb45ff324d530a8a340cc220c8d78c3a82eb0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp34-cp34m-win32.whl
  • Upload date:
  • Size: 108.4 kB
  • Tags: CPython 3.4m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 937049800ab159f2c205d12ea8423cd6ff94b79de835f525a450fb7dd55e3d00
MD5 0acc100aaadc3bd0d17fce733973a255
BLAKE2b-256 c4b5f5bad308555cc26dc1c43731fddd1756626ee0ddab0bb80294a0fed0ad7a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp27-cp27m-win_amd64.whl
  • Upload date:
  • Size: 117.7 kB
  • Tags: CPython 2.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 94c038952b40fc28a019165b26e1e6e99390e74e6e86651e82549c77e0b759c6
MD5 0bdf030a67f53fd533fe08e8d8c3e51f
BLAKE2b-256 e6c9ccb56b4a37b09b32225d813fd47bd574965dcd3306732cf34e93044f681a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primesieve-1.4.4-cp27-cp27m-win32.whl
  • Upload date:
  • Size: 106.3 kB
  • Tags: CPython 2.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.5 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/2.7.10

File hashes

Hashes for primesieve-1.4.4-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 7dad439f1473cb9cb4d77f26e78eb854381eabe5b83381af9b5ba42e3ee2d970
MD5 31418e4c5528a6ddc45bc06e22c1a5c7
BLAKE2b-256 5d71e8aed9e6af066019dbcbe2434dcbfcc66a74b72e21cddec5ce66d0a64fb7

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