Skip to main content

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

Project description

primesieve-python
=================
[![Build Status](https://travis-ci.org/hickford/primesieve-python.svg?branch=master)](https://travis-ci.org/hickford/primesieve-python) [![Build status](https://ci.appveyor.com/api/projects/status/4chekgdj7bqx4ivt/branch/master?svg=true)](https://ci.appveyor.com/project/hickford/primesieve-python/branch/master) [![PyPI](https://img.shields.io/pypi/v/primesieve.svg)](https://pypi-hypernode.com/pypi/primesieve) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/hickford/primesieve-python/blob/master/LICENSE)

Python bindings for the [primesieve](http://primesieve.org/) 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](https://en.wikipedia.org/wiki/Prime_k-tuple)
* 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.

```bash
# 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
--------------

```Python
>>> 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](primesieve/_primesieve.pyx).

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.

```Python
>>> 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.

```Python
>>> 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](#prerequisites).

```bash
# 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.0.tar.gz (250.0 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.6m Windows x86-64

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

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.5m Windows x86-64

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

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.4m Windows x86-64

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

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 2.7m Windows x86-64

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

Uploaded CPython 2.7m Windows x86

File details

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

File metadata

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

File hashes

Hashes for primesieve-1.4.0.tar.gz
Algorithm Hash digest
SHA256 db39866ce5003311bc0e3eeeda7e77b0f70816f74795d6e68401fb4dbf531402
MD5 427d2f64eba6f1685f239e42d687db29
BLAKE2b-256 1a5ae7a62e3202f307bd02f3dc5538e20fa7f3ee5ffb1bb07304e43a77ad0879

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 5f1d99dba10fc1009c923a4ca667ea5d6a9d87dd27961457ae89329a549c33d5
MD5 4819a7eed46b0dfc550e51720bdf2492
BLAKE2b-256 f249ea7f674ae6d3fe55641151d4e95b3fb1d115d4cba9047156a55147d49fd3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 6acea89f97fa8c31829ace9a7ae74631f167ff0a42dbb6277e695593976aef5e
MD5 35906b25dd9ab64619a6170e5edb0b1d
BLAKE2b-256 e6dc339898bfa29be514ee03b02a4d9136678b23f1fc6b61decf8ac527f52e73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 99cd0bb4ebe4972bcbcb1e764344e62002ae5cccc826e401d427ce65da865e25
MD5 82c6646ce68ed428d58011e4246ed466
BLAKE2b-256 af352e29f8cc634f5e1b014a52fc37713cf97d2d4ef279c0d775ac6c6e105e86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 635db9816a7155f26b0db4122545073322618b0b9a67374e3f85e992b12a0a03
MD5 fab57946fc9e4d857b58882281707884
BLAKE2b-256 eb5e46ff5e65471f39bc13ddb15cd85cca2070dfaad888808d9080adff22ebc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 7916083b4f2e620c03058a3964da9bd901e01026bafb9560ae6845fc6aa241ee
MD5 72660b5bc71374f6cc685b6a1e3fd8f9
BLAKE2b-256 5d1ff2617407aa7cfee856704964f16199a7b4e6e2fbec05c055f862d48da46c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 2946632b2a5bd7a6d13873c0f67c1db9af1ab74043e9cfceb866026adf6024d3
MD5 8b942c2e6c465834de8411d2ebe2658b
BLAKE2b-256 80716680e59627a979f3a1ad20e5b600f28231328b384579064f6a5fbbbd9e66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 90bb5736d0a80cc803a0721af479c627e9a57b2f466bfed87b7ccb3314ee529a
MD5 f0e8126361f2f6aafb3c3f9f61631ee5
BLAKE2b-256 42cf9b01bceb702408e027b4557e893b6bc66f57c997dd0d03e24f0ab4a02a80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primesieve-1.4.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 157afba2c922aa439d2dec07062046b9004ea0f88989ed04993d4541cbc8a11b
MD5 961cab524f16dab2364e973783ad3447
BLAKE2b-256 db901ea33b55e82490e0512a0cf9d5074b078b4bc39624cfb243ae4e4fdddf19

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