libcpprime
libcpprime is an efficient C++ implementation of a primality test optimized for 64-bit integers.
Usage
cppr::IsPrime()
Header: <libcpprime/IsPrime.hpp>
namespace cppr {
bool IsPrime(std::uint64_t n) noexcept; // C++11
constexpr bool IsPrime(std::uint64_t n) noexcept; // C++20
}
It returns true if the input value is a prime number; otherwise, it returns false.
example
#include <libcpprime/IsPrime.hpp>
#include <cassert>
int main() {
assert(cppr::IsPrime(998244353) == true);
assert(cppr::IsPrime(13148563482635885461) == false);
}
cppr::IsPrimeNoTable
Header: <libcpprime/IsPrimeNoTable.hpp>
namespace cppr {
bool IsPrimeNoTable(std::uint64_t n) noexcept; // C++11
constexpr bool IsPrimeNoTable(std::uint64_t n) noexcept; // C++20
}
It returns true if the input value is a prime number; otherwise, it returns false. If you want to reduce the size of the executable file, use this function instead of cppr::IsPrime because cppr::IsPrime uses a 40KB table for performance optimization.
example
#include <libcpprime/IsPrimeNoTable.hpp>
#include <cassert>
int main() {
assert(cppr::IsPrimeNoTable(998244353) == true);
assert(cppr::IsPrimeNoTable(1314856348263588546) == false);
}
CPPR_HAS_CONSTEXPR_IS_PRIME
Header: <libcpprime/FeatureTestMacros.hpp>
#define CPPR_HAS_CONSTEXPR_IS_PRIME 1 // C++20
This is a feature test macro that determines whether cppr::IsPrime and cppr::IsPrimeNoTable are declared with constexpr.
example
#include <libcpprime/FeatureTestMacros.hpp>
#include <libcpprime/IsPrime.hpp>
#include <iostream>
int main() {
#ifdef CPPR_HAS_CONSTEXPR_IS_PRIME
constexpr bool x = cppr::IsPrime(1000000007);
#else
const bool x = cppr::IsPrime(1000000007);
#endif
std::cout << x << std::endl;
}
Requirements
- C++11
- GCC, Clang, GCC (MinGW), Clang (MinGW), MSVC, clang-cl
Compilation
This library is header-only, so you only need to specify the include path.
g++ -I ./libcpprime -O3 Main.cpp
Benchmarks
Benchmarks are executed on GitHub Actions.
- Workflow: https://github.com/Rac75116/libcpprime/actions/workflows/bench.yml
- Latest results: https://rac75116.github.io/libcpprime/benchmarks/latest.json
Linux (gcc)
- summary.md: https://rac75116.github.io/libcpprime/benchmarks/latest/benchmark-Linux-gcc/bench_summary.md



Linux (clang)
- summary.md: https://rac75116.github.io/libcpprime/benchmarks/latest/benchmark-Linux-clang/bench_summary.md



Windows (msvc)
- summary.md: https://rac75116.github.io/libcpprime/benchmarks/latest/benchmark-Windows-msvc/bench_summary.md



Windows (clang-cl)
- summary.md: https://rac75116.github.io/libcpprime/benchmarks/latest/benchmark-Windows-clang-cl/bench_summary.md



Releases
- 2025/12/24 ver 1.3.1
- Improve performance and reduce binary size for
cppr::IsPrime
- Improve performance and reduce binary size for
- 2025/12/21 ver 1.3.0
- Add
CPPR_HAS_CONSTEXPR_IS_PRIME - Support clang-cl
- Accelerating Compile-Time Computation
- Improved compatibility
- Add
- 2025/03/10 ver 1.2.11
- Change the name on the license
- Change Multiprication Algorithm
- Replace
__uint128_twithunsigned __int128
- 2025/01/05 ver 1.2.10
- Change the condition of
constexpr
- Change the condition of
- 2025/01/03 ver 1.2.9
- Fix a bug
- 2025/01/02 ver 1.2.8
- Improve performance
- Suppress warnings
- 2024/12/31 ver 1.2.7
- Improve performance
- 2024/12/30 ver 1.2.6
- Improve performance
- 2024/12/29 ver 1.2.5
- Add copyrights notice
- 2024/12/28 ver 1.2.4
- Improve performance
- 2024/12/26 ver 1.2.3
- Improve performance
- 2024/12/25 ver 1.2.2
- Improve performance
- 2024/12/23 ver 1.2.1
- Improve performance
- 2024/12/19 ver 1.2.0
- Split
cppr::IsPrimeintocppr::IsPrimeandcppr::IsPrimeNoTable
- Split
- 2024/12/19 ver 1.1.2
- Fix typo
- 2024/12/18 ver 1.1.1
- Add include guards
- 2024/12/18 ver 1.1.0
- Add
cppr::IsPrimewith a table
- Add
- 2024/12/18 ver 1.0.0
- Add
cppr::IsPrime
- Add