1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
primesieve
==========
[](https://travis-ci.org/kimwalisch/primesieve)
[](https://ci.appveyor.com/project/kimwalisch/primesieve)
[](https://github.com/kimwalisch/primesieve/blob/master/COPYING)
primesieve is a program and C/C++ library that generates primes using a highly optimized
<a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">sieve of
Eratosthenes</a> implementation. It counts the primes below 10^10 in
just 0.45 seconds on an Intel Core i7-6700 CPU (4 x 3.4GHz).
primesieve can generate primes and
<a href="http://en.wikipedia.org/wiki/Prime_k-tuple">prime k-tuplets</a>
up to 2^64.
- **Homepage:** http://primesieve.org
- **Binaries:** http://primesieve.org/downloads
- **API:** http://primesieve.org/api

Algorithm complexity
--------------------
primesieve generates primes using the segmented sieve of Eratosthenes with
[wheel factorization](http://en.wikipedia.org/wiki/Wheel_factorization).
This algorithm has a run time complexity of
<img src="http://primesieve.org/images/Onloglogn.svg" height="20" align="absmiddle"/>
operations and uses
<img src="http://primesieve.org/images/Osqrtn.svg" height="20" align="absmiddle"/>
memory. Furthermore primesieve uses the
[bucket sieve](http://sweet.ua.pt/tos/software/prime_sieve.html)
algorithm for large sieving primes which reduces the memory usage to
<img src="http://primesieve.org/images/primesieve_memory_usage.svg" height="20" align="absmiddle"/>
bytes per thread.
Package managers
----------------
The primesieve console application can be installed using your operating
system's package manager. The primesieve GUI application can be
downloaded from
[http://primesieve.org/downloads](http://primesieve.org/downloads).
```sh
# Debian/Ubuntu
sudo apt-get install primesieve
# macOS
brew install primesieve
```
Console application
-------------------
The primesieve console application can generate primes and prime
k-tuplets.
```sh
# Count the primes below 1e10 using all CPU cores
primesieve 1e10
# Print the primes below 1000000
primesieve 1000000 --print
# Print the twin primes below 1000000
primesieve 1000000 --print=2
# Count the primes within [1e10, 2e10] using 4 threads
primesieve 1e10 2e10 --threads=4
# Print an option summary
primesieve --help
```
Build requirements
------------------
primesieve is very portable, it compiles with every C++ compiler and
runs on most CPU architectures out there. The parallelization is
implemented using [OpenMP](http://en.wikipedia.org/wiki/OpenMP). The
primesieve GUI application (not built by default) uses the
[Qt framework](http://qt-project.org).
primesieve is also a library, it natively supports C and C++ and there
are [bindings](#bindings-for-other-languages) available for a few
other programming languages. The author's
[primecount](https://github.com/kimwalisch/primecount) program uses
libprimesieve extensively.
Build instructions (Unix-like OSes)
-----------------------------------
Download [primesieve-5.7.2.tar.gz](https://bintray.com/artifact/download/kimwalisch/primesieve/primesieve-5.7.2.tar.gz)
and extract it, then build primesieve using:
```sh
./configure
make
sudo make install
```
If you have downloaded a zip archive from GitHub then Autotools
(a.k.a. the GNU Build System) must be installed and ```autogen.sh``` must
be executed once. To install Autotools install
[GNU Autoconf](http://www.gnu.org/software/autoconf/),
[GNU Automake](http://www.gnu.org/software/automake/) and
[GNU Libtool](http://www.gnu.org/software/libtool/)
using your operating system's package manager.
```sh
./autogen.sh
./configure
make
sudo make install
```
To enable building the example programs use:
```sh
./configure --enable-examples
```
Build instructions (Microsoft Visual C++)
-----------------------------------------
Download
[primesieve-5.7.2.zip](https://bintray.com/artifact/download/kimwalisch/primesieve/primesieve-5.7.2.zip)
and extract it. Then open a Visual Studio Command Prompt and run:
```sh
nmake -f Makefile.msvc
```
To build the example programs use:
```sh
nmake -f Makefile.msvc examples
```
C++ API
-------
Below is an example with the most common libprimesieve use cases.
```C++
#include <primesieve.hpp>
#include <iostream>
#include <vector>
int main()
{
// store the primes below 1000
std::vector<int> primes;
primesieve::generate_primes(1000, &primes);
primesieve::iterator it;
uint64_t prime = it.next_prime();
// iterate over the primes below 10^6
for (; prime < 1000000; prime = it.next_prime())
std::cout << prime << std::endl;
return 0;
}
```
* [More C++ examples](examples/cpp)
* [Browse primesieve's C++ API online](http://primesieve.org/api/primesieve_8hpp.html)
C API
-----
primesieve's functions are exposed as C API via the ```primesieve.h```
header.
```C
#include <primesieve.h>
#include <stdio.h>
int main()
{
primesieve_iterator it;
primesieve_init(&it);
uint64_t prime;
/* iterate over the primes below 10^6 */
while ((prime = primesieve_next_prime(&it)) < 1000000)
printf("%llu\n", prime);
primesieve_free_iterator(&it);
return 0;
}
```
* [More C examples](examples/c)
* [Browse primesieve's C API online](http://primesieve.org/api/primesieve_8h.html)
Linking against libprimesieve
-----------------------------
**Unix-like operating systems**
```sh
c++ -O2 primes.cpp -lprimesieve
cc -O2 primes.c -lprimesieve
```
If you have built primesieve yourself then the default installation path is
```/usr/local/lib``` which is not part of ```LD_LIBRARY_PATH``` on many
OSes. Hence you need to export some environment variables:
```sh
export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export CPLUS_INCLUDE_PATH=/usr/local/include:$CPLUS_INCLUDE_PATH
export C_INCLUDE_PATH=/usr/local/include:$C_INCLUDE_PATH
```
**Microsoft Visual C++ (Windows)**
```
cl /O2 /EHsc primes.cpp /I primesieve\include /link primesieve\primesieve.lib
```
Bindings for other languages
----------------------------
primesieve supports C++ and C directly, and has bindings available for
a few other languages:
<table>
<tr>
<td><b>Python:</b></td>
<td><a href="https://github.com/hickford/primesieve-python">primesieve-python</a></td>
</tr>
<tr>
<td><b>Ruby:</b></td>
<td><a href="https://github.com/robertjlooby/primesieve-ruby">primesieve-ruby</a></td>
</tr>
</table>
Many thanks to the developers of these bindings!
|