Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

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


Release history Release notifications

This version
History Node

1.4.3

History Node

1.4.1

History Node

1.4.0

History Node

1.3.0

History Node

1.2.0

History Node

1.1.0

History Node

1.0.0

History Node

0.1.2

History Node

0.1.0

History Node

0.0.2

History Node

0.0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
primesieve-1.4.3-cp27-cp27m-win32.whl (106.3 kB) Copy SHA256 hash SHA256 Wheel cp27 Jun 23, 2018
primesieve-1.4.3-cp27-cp27m-win_amd64.whl (117.6 kB) Copy SHA256 hash SHA256 Wheel cp27 Jun 23, 2018
primesieve-1.4.3-cp34-cp34m-win32.whl (108.2 kB) Copy SHA256 hash SHA256 Wheel cp34 Jun 23, 2018
primesieve-1.4.3-cp34-cp34m-win_amd64.whl (115.8 kB) Copy SHA256 hash SHA256 Wheel cp34 Jun 23, 2018
primesieve-1.4.3-cp35-cp35m-win32.whl (96.4 kB) Copy SHA256 hash SHA256 Wheel cp35 Jun 23, 2018
primesieve-1.4.3-cp35-cp35m-win_amd64.whl (106.1 kB) Copy SHA256 hash SHA256 Wheel cp35 Jun 23, 2018
primesieve-1.4.3-cp36-cp36m-win32.whl (96.6 kB) Copy SHA256 hash SHA256 Wheel cp36 Jun 23, 2018
primesieve-1.4.3-cp36-cp36m-win_amd64.whl (106.3 kB) Copy SHA256 hash SHA256 Wheel cp36 Jun 23, 2018
primesieve-1.4.3.tar.gz (198.3 kB) Copy SHA256 hash SHA256 Source None Jun 23, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page