项目作者: kimwalisch

项目描述 :
🚀 Fast prime number generator
高级语言: C++
项目地址: git://github.com/kimwalisch/primesieve.git
创建时间: 2013-09-24T17:58:41Z
项目社区:https://github.com/kimwalisch/primesieve

开源协议:BSD 2-Clause "Simplified" License

下载


primesieve

Build status
Github Releases
C API Documentation
C++ API Documentation

primesieve is a command-line program and C/C++ library for quickly generating prime numbers.
It is very cache efficient, it detects your CPU’s L1 & L2 cache sizes and allocates its main
data structures accordingly. It is also multi-threaded by default, it uses all available CPU
cores whenever possible i.e. if sequential ordering is not required. primesieve
can generate primes and prime k-tuplets
up to 264.

primesieve generates primes using the segmented
sieve of Eratosthenes with
wheel factorization.
This algorithm has a run time complexity of $O(n\ \log\ \log\ n)$ operations and uses
$O(\sqrt{n})$ memory. Furthermore primesieve uses the
bucket sieve
algorithm which improves the cache efficiency when generating primes > 232.
primesieve uses 8 bytes per sieving prime, in practice its memory usage is about
$\pi(\sqrt{n})\times 8$ bytes per thread.

Installation

The primesieve command-line program can be installed using your operating system’s
package manager. For doing development with libprimesieve you may need
to install libprimesieve-dev or libprimesieve-devel.






























Windows: winget install primesieve
macOS: brew install primesieve
Arch Linux: sudo pacman -S primesieve
Debian/Ubuntu: sudo apt install primesieve
Fedora: sudo dnf install primesieve
FreeBSD: pkg install primesieve
openSUSE: sudo zypper install primesieve

Usage examples

  1. # Count the primes ≤ 1e10 using all CPU cores
  2. primesieve 1e10
  3. # Print the primes ≤ 1000000
  4. primesieve 1000000 --print
  5. # Store the primes ≤ 1000000 in a text file
  6. primesieve 1000000 --print > primes.txt
  7. # Print the twin primes ≤ 1000000
  8. primesieve 1000000 --print=2
  9. # Count the prime triplets inside [1e10, 1e10+2^32]
  10. primesieve 1e10 --dist=2^32 --count=3

Stress testing

primesieve includes support for stress testing both the CPU and memory. This feature
is useful for checking system stability under maximum load and verifying whether
your cooling solution (fans, heatsinks, thermal paste, etc.) is adequate. primesieve’s
stress test supports two modes: CPU (highest CPU load, uses little memory) and
RAM (high memory usage, uses about 1.2 GiB per thread).

  1. $ primesieve --stress-test --timeout 5m
  2. Started CPU stress testing using 14 threads.
  3. The expected memory usage is: 14 threads * 2.85 MiB = 39.90 MiB.
  4. The stress test keeps on running until either a miscalculation occurs
  5. (due to a hardware issue) or the timeout of 5m expires.
  6. You may cancel the stress test at any time using Ctrl+C.
  7. [Apr 12 19:09] Thread 14, 25.19 secs, PrimePi(1e13+98e11, 1e13+99e11) = 3265923128 OK
  8. [Apr 12 19:09] Thread 9, 32.48 secs, PrimePi(1e13+63e11, 1e13+64e11) = 3286757785 OK
  9. [Apr 12 19:09] Thread 14, 22.32 secs, PrimePi(1e13+ 0e11, 1e13+ 1e11) = 3340141707 OK
  10. [Apr 12 19:10] Thread 2, 17.10 secs, PrimePi(1e13+16e11, 1e13+17e11) = 3323791292 OK
  11. [Apr 12 19:10] Thread 14, 18.45 secs, PrimePi(1e13+ 3e11, 1e13+ 4e11) = 3336895789 OK
  12. [Apr 12 19:11] Thread 13, 16.96 secs, PrimePi(1e13+96e11, 1e13+97e11) = 3267004191 OK
  13. [Apr 12 19:11] Thread 2, 31.79 secs, PrimePi(1e13+20e11, 1e13+21e11) = 3320071119 OK
  14. [Apr 12 19:12] Thread 11, 29.96 secs, PrimePi(1e13+84e11, 1e13+85e11) = 3273743021 OK
  15. [Apr 12 19:13] Thread 4, 32.45 secs, PrimePi(1e13+37e11, 1e13+38e11) = 3305523133 OK
  16. All tests passed successfully!

Command-line options

  1. Usage: primesieve [START] STOP [OPTION]...
  2. Generate the primes and/or prime k-tuplets inside [START, STOP]
  3. (< 2^64) using the segmented sieve of Eratosthenes.
  4. Options:
  5. -c, --count[=NUM+] Count primes and/or prime k-tuplets, NUM <= 6.
  6. Count primes: -c or --count (default option),
  7. count twin primes: -c2 or --count=2,
  8. count prime triplets: -c3 or --count=3, ...
  9. --cpu-info Print CPU information (cache sizes).
  10. -d, --dist=DIST Sieve the interval [START, START + DIST].
  11. -n, --nth-prime Find the nth prime.
  12. primesieve 100 -n: finds the 100th prime,
  13. primesieve 2 100 -n: finds the 2nd prime > 100.
  14. -p, --print[=NUM] Print primes or prime k-tuplets, NUM <= 6.
  15. Print primes: -p or --print,
  16. print twin primes: -p2 or --print=2,
  17. print prime triplets: -p3 or --print=3, ...
  18. -q, --quiet Quiet mode, prints less output.
  19. -s, --size=SIZE Set the sieve size in KiB, SIZE <= 8192.
  20. By default primesieve uses a sieve size that
  21. matches your CPU's L1 cache size (per core) or is
  22. slightly smaller than your CPU's L2 cache size.
  23. -S, --stress-test[=MODE] Run a stress test. The MODE can be either
  24. CPU (default) or RAM. The default timeout is 24h.
  25. --test Run various correctness tests (< 1 minute).
  26. -t, --threads=NUM Set the number of threads, NUM <= CPU cores.
  27. Default setting: use all available CPU cores.
  28. --time Print the time elapsed in seconds.
  29. --timeout=SEC Set the stress test timeout in seconds. Supported
  30. units of time suffixes: s, m, h, d or y.
  31. 30 minutes timeout: --timeout 30m

Build instructions

You need to have installed a C++ compiler which supports C++11 (or later)
and CMake ≥ 3.4.

  1. cmake .
  2. cmake --build . --parallel
  3. sudo cmake --install .
  4. sudo ldconfig

C API

Include the <primesieve.h> header to use libprimesieve’s C API.

  1. #include <primesieve.h>
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. int main()
  5. {
  6. primesieve_iterator it;
  7. primesieve_init(&it);
  8. uint64_t prime;
  9. /* Iterate over the primes < 10^6 */
  10. while ((prime = primesieve_next_prime(&it)) < 1000000)
  11. printf("%" PRIu64 "\n", prime);
  12. primesieve_free_iterator(&it);
  13. return 0;
  14. }

C++ API

Include the <primesieve.hpp> header to use libprimesieve’s C++ API.

  1. #include <primesieve.hpp>
  2. #include <iostream>
  3. int main()
  4. {
  5. primesieve::iterator it;
  6. uint64_t prime = it.next_prime();
  7. // Iterate over the primes < 10^6
  8. for (; prime < 1000000; prime = it.next_prime())
  9. std::cout << prime << std::endl;
  10. return 0;
  11. }

Bindings for other languages

primesieve natively supports C and C++ and has bindings available for:






















































Common Lisp: cl-primesieve
Java: primesieve-java
Janet: janet-primesieve
Julia: PrimeSieve.jl
Lua: lua-primesieve
Nim: primesievec-nim
Haskell: primesieve-haskell
Pascal: primesieve-pas
Perl: Primesieve
Python: primesieve-python
Raku: raku-primesieve
Ruby: primesieve-ruby
Rust: primesieve.rs

Many thanks to the developers of these bindings!

Sponsors

Thanks to all current and past sponsors of primesieve! Your donations help me purchase (or rent) the latest CPUs and ensure primesieve runs at maximum performance on them. Your donations also motivate me to continue maintaining primesieve.