|
| 1 | +# primesieve-python |
| 2 | + |
| 3 | +[](https://travis-ci.org/kimwalisch/primesieve-python) [](https://pypi.python.org/pypi/primesieve) |
| 4 | + |
| 5 | +# Summary |
| 6 | + |
| 7 | +Python bindings for the [primesieve](https://github.com/kimwalisch/primesieve) |
| 8 | +C++ library. |
| 9 | + |
| 10 | +Generates primes orders of magnitude faster than any pure Python code! |
| 11 | + |
| 12 | +**Features:** |
| 13 | + |
| 14 | +* Get an array of primes |
| 15 | +* Iterate over primes using little memory |
| 16 | +* Find the nth prime |
| 17 | +* Count/print primes and [prime k-tuplets](https://en.wikipedia.org/wiki/Prime_k-tuple) |
| 18 | +* Multi-threaded for counting primes and finding the nth prime |
| 19 | +* NumPy support |
| 20 | + |
| 21 | +# Prerequisites |
| 22 | + |
| 23 | +We provide primesieve wheels (distribution packages) for Windows, macOS |
| 24 | +and Linux for x86 and x64 CPUs. For other operating systems and/or CPUs |
| 25 | +you need to have installed a C++ compiler. |
| 26 | + |
| 27 | +```bash |
| 28 | +# Ubuntu/Debian |
| 29 | +sudo apt install g++ python-dev |
| 30 | + |
| 31 | +# Fedora |
| 32 | +sudo dnf install gcc-c++ python-devel |
| 33 | + |
| 34 | +# macOS |
| 35 | +xcode-select --install |
| 36 | +``` |
| 37 | + |
| 38 | +# Installation |
| 39 | + |
| 40 | +```bash |
| 41 | +# Python 3.5 or later |
| 42 | +pip install primesieve |
| 43 | + |
| 44 | +# For Python 2.7 use: |
| 45 | +pip install "primesieve<=1.4.4" |
| 46 | +``` |
| 47 | + |
| 48 | +# Conda Installation |
| 49 | + |
| 50 | +[](https://travis-ci.org/conda-forge/python-primesieve-feedstock) |
| 51 | +[](https://ci.appveyor.com/project/conda-forge/python-primesieve-feedstock/branch/master) |
| 52 | +[](https://circleci.com/gh/conda-forge/python-primesieve-feedstock) |
| 53 | +[](https://anaconda.org/conda-forge/python-primesieve) |
| 54 | + |
| 55 | +You don't need to install a C++ compiler when installing python-primesieve using Conda. |
| 56 | + |
| 57 | +``` |
| 58 | +conda install -c conda-forge python-primesieve |
| 59 | +``` |
| 60 | + |
| 61 | +# Usage examples |
| 62 | + |
| 63 | +```Python |
| 64 | +>>> from primesieve import * |
| 65 | + |
| 66 | +# Get an array of the primes <= 40 |
| 67 | +>>> primes(40) |
| 68 | +[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] |
| 69 | + |
| 70 | +# Get an array of the primes between 100 and 120 |
| 71 | +>>> primes(100, 120) |
| 72 | +[101, 103, 107, 109, 113] |
| 73 | + |
| 74 | +# Get an array of the first 10 primes |
| 75 | +>>> n_primes(10) |
| 76 | +[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
| 77 | + |
| 78 | +# Get an array of the first 10 primes >= 1000 |
| 79 | +>>> n_primes(10, 1000) |
| 80 | +[1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061] |
| 81 | + |
| 82 | +# Get the 10th prime |
| 83 | +>>> nth_prime(10) |
| 84 | +29 |
| 85 | + |
| 86 | +# Count the primes below 10**9 |
| 87 | +>>> count_primes(10**9) |
| 88 | +50847534 |
| 89 | +``` |
| 90 | + |
| 91 | +Here is a [list of all available functions](primesieve/_primesieve.pyx). |
| 92 | + |
| 93 | +# Iterating over primes |
| 94 | + |
| 95 | +Instead of generating a large array of primes and then do something |
| 96 | +with the primes it is also possible to simply iterate over the primes |
| 97 | +which uses less memory. |
| 98 | + |
| 99 | +```Python |
| 100 | +""">>> import primesieve |
| 101 | +
|
| 102 | +it = primesieve.Iterator() |
| 103 | +prime = it.next_prime() |
| 104 | +
|
| 105 | +# Iterate over the primes below 10000 |
| 106 | +while prime < 10000: |
| 107 | + print prime |
| 108 | + prime = it.next_prime() |
| 109 | +
|
| 110 | +# Set iterator start number to 100 |
| 111 | +it.skipto(100) |
| 112 | +prime = it.prev_prime() |
| 113 | +
|
| 114 | +# Iterate backwards over the primes below 100 |
| 115 | +while prime > 0: |
| 116 | + print prime |
| 117 | + prime = it.prev_prime()""" |
| 118 | + |
| 119 | +from primesieve import primes_range |
| 120 | + |
| 121 | +# iterate over the primes in range between 100 and 10^7 |
| 122 | +# 100 < prime <= 10^7 |
| 123 | +for prime in primes_range(100,10e7): |
| 124 | + |
| 125 | + # do something with the prime |
| 126 | + ... |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +# NumPy support |
| 131 | + |
| 132 | +Using the ```primesieve.numpy``` module you can generate an array of |
| 133 | +primes using native C++ performance! |
| 134 | + |
| 135 | +In comparison the ```primesieve``` module generates an array of primes |
| 136 | +about 3 times slower mostly because the conversion of the C primes |
| 137 | +array into a python array is quite slow. |
| 138 | + |
| 139 | +```Python |
| 140 | +>>> from primesieve.numpy import * |
| 141 | + |
| 142 | +# Generate a numpy array with the primes below 100 |
| 143 | +>>> primes(100) |
| 144 | +array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, |
| 145 | + 61, 67, 71, 73, 79, 83, 89, 97]) |
| 146 | + |
| 147 | +# Generate a numpy array with the first 100 primes |
| 148 | +>>> n_primes(100) |
| 149 | +array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, |
| 150 | + 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, |
| 151 | + 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, |
| 152 | + 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, |
| 153 | + 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, |
| 154 | + 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, |
| 155 | + 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, |
| 156 | + 479, 487, 491, 499, 503, 509, 521, 523, 541]) |
| 157 | +``` |
| 158 | + |
| 159 | +# Development |
| 160 | + |
| 161 | +You need to have installed a C++ compiler, see [Prerequisites](#prerequisites). |
| 162 | + |
| 163 | +```bash |
| 164 | +# Install prerequisites |
| 165 | +pip install cython pytest numpy |
| 166 | + |
| 167 | +# Clone repository |
| 168 | +git clone --recursive https://github.com/kimwalisch/primesieve-python |
| 169 | + |
| 170 | +cd primesieve-python |
| 171 | + |
| 172 | +# Build and install primesieve-python |
| 173 | +pip install . --upgrade |
| 174 | + |
| 175 | +# Run tests |
| 176 | +pytest |
| 177 | +``` |
| 178 | + |
| 179 | +# How to do a new release |
| 180 | + |
| 181 | +* You need to be a maintainer of the [primesieve-python](https://github.com/kimwalisch/primesieve-python) repo. |
| 182 | +* You need to be a maintainer of the [primesieve pypi](https://pypi.org/project/primesieve) project. |
| 183 | +* Compare ```.travis.yml``` with [cibuildwheel#example-setup](https://github.com/joerick/cibuildwheel#example-setup) and update ```.travis.yml``` if needed. |
| 184 | +* Update the supported Python versions in ```setup.py``` (we support the same versions as [cibuildwheel](https://pypi.org/project/cibuildwheel)). |
| 185 | +* Increment the primesieve-python version in ```setup.py```. Ideally this should be the last commit before the release as this uploads the new primesieve wheels to [https://test.pypi.org](https://test.pypi.org/project/primesieve/#files). |
| 186 | +* Check if all primesieve wheels (Windows, macOS, Linux) have been uploaded to [https://test.pypi.org](https://test.pypi.org/project/primesieve/#files). |
| 187 | +* If not, read the [Travis CI logs](https://travis-ci.org/github/kimwalisch/primesieve-python) and fix the bugs. |
| 188 | +* Finally, do a new release on GitHub. |
0 commit comments