Giter Club home page Giter Club logo

primesieve's Introduction

primesieve

Build Status Build Status GitHub license

primesieve is a program and C/C++ library that generates primes using a highly optimized sieve of Eratosthenes 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 prime k-tuplets up to 2^64.

primesieve windows screenshot

Algorithm complexity

primesieve generates primes using the segmented sieve of Eratosthenes with wheel factorization, this algorithm has a complexity of operations and uses space. More precisely primesieve's memory usage per thread is about bytes.

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. The primesieve GUI application (not built by default) uses the Qt framework.

primesieve is also a library, it supports C++ and C directly and there are bindings available for a few other programming languages. The author's primecount program uses libprimesieve extensively.

Build instructions (Unix-like OSes)

Download primesieve-5.6.0.tar.gz and extract it. Then build primesieve using:

$ ./configure
$ make
$ sudo make install

If you have downloaded a zip archive from GitHub then Autotools (a.k.a. GNU Build System) must be installed and autogen.sh must be executed once. To install Autotools install GNU Autoconf, GNU Automake and GNU Libtool using your operating system's package manager.

$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

To enable building the example programs use:

$ ./configure --enable-examples

Build instructions (Microsoft Visual C++)

Open a Visual Studio Command Prompt, cd into the primesieve directory and run:

> nmake -f Makefile.msvc

To build the example programs use:

> nmake -f Makefile.msvc examples

Console application

The primesieve console application can print and count primes and prime k-tuplets and find the nth prime.

# Print the primes below 1000000
$ primesieve 1000000 --print

# Print the twin primes below 1000000
$ primesieve 1000000 --print=2

# Count the primes below 1e10 using all CPU cores
$ primesieve 1e10 --count

# Count the primes within [1e10, 2e10] using 4 threads
$ primesieve 1e10 2e10 --count --threads=4

# Print an option summary
$ primesieve --help

C++ API

After having installed primesieve you can easily use it in your C++ program, below is an example. primesieve's API is documented online at http://primesieve.org/api.

#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 pi;
  uint64_t prime;

  // iterate over the primes below 10^9
  while ((prime = pi.next_prime()) < 1000000000)
    std::cout << prime << std::endl;

  return 0;
}

C API

All of primesieve's functions are exposed as C API (C99 or later) via the primesieve.h header. You can browse primesieve's C API online at http://primesieve.org/api.

#include <primesieve.h>
#include <stdint.h>
#include <stdio.h>

int main()
{
  uint64_t start = 0;
  uint64_t stop = 10000;
  size_t i;
  size_t size;

  /* get an array with primes below 10000 */
  int* primes = (int*) primesieve_generate_primes(start, stop, &size, INT_PRIMES);

  for (i = 0; i < size; i++)
    printf("%i\n", primes[i]);

  /* deallocate primes array generated using primesieve */
  primesieve_free(primes);
  return 0;
}

Linking against libprimesieve

Unix-like operating systems

$ c++ -O2 primes.cpp -lprimesieve
$ cc  -O2 primes.c   -lprimesieve

Note that if you have built primesieve yourself the default installation path is /usr/local/lib which is not part of LD_LIBRARY_PATH on many systems. Hence you need to export some additional environment variables:

$ 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:

Python: primesieve-python
Ruby: primesieve-ruby

Many thanks to the developers of these bindings!

primesieve's People

Contributors

kimwalisch avatar ohanar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.