prime:: namespace
function templates

This is version 0.4 of the prime:: namespace function templates.




  • Easy. The function template accepts all reasonable data types. No compilation and linking of object files needed.
  • Fast. Multithreading is implemented and for integrals up to a size of 32 bits you can enjoy an optimized algorithm that provides extremely fast results.
  • Safe. Latest c++ features are used to solve the problem of ambiguity during overload resolution. You will always get a correct return value, no matter what data type and value you call the function with: If it compiles, it works!
  • Free. This software is published under the GNU General Public Licence version 3.
  • Extensively documented. The manual provides detailed information about the algorithms used, theoretic considerations, measurements and a few remarks that could be of value as well for learners as for advanced programmers.

A short hands on manual for beginners

1. Include. In the head of your program or function that uses a function from this template include the downloaded file prime.hpp as you include any other header file that is not part of the standard library. Bear in mind to use "" around the file name and not <> as you may be used from the standard library. Do not worry about multiple includes. A header guard named PRIME_HPP will take care of that.

#include <iostream>
#include "prime.hpp"

2. Usage. For a fast and easy start I recommend to use variables of the type unsigned long long int. To deal with numbers of any size or to benefit from optimizations see the complete manual. The following code shows how to use the functions in your program:

int main()
    unsigned long long int n = 11;
    std::cout << std::boolalpha;
    std::cout << prime::isprime(n) << std::endl;
    // outputs "true"
    n = prime::nextprime(n);
    std::cout << n << std::endl;
    // outputs "13"
    n = prime::prevprime(n);
    std::cout << n << std::endl;
    // outputs "11"
    return 0;

3. Compilation. To compile the code with g++ or clang++ make sure to use the compiler option -std=c++17 to take advantage of the latest c++ features without compiler warning, -pthread to compile for multithreading and probably -O3 to get well optimized machine code. These options are the same for both compilers mentioned.

You do not need to build any object file and link it. Just compile and the functions will be build according to your call:

g++ -std=c++17 -pthread -O3 myprogram.cpp

Have fun!