Skip to content

Rac75116/libcpprime

Repository files navigation

libcpprime

badge

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: bench.yml

Linux (gcc)

View Summary

Linux gcc summary Linux gcc IsPrime Linux gcc IsPrimeNoTable

Linux (clang)

View summary

Linux clang summary Linux clang IsPrime Linux clang IsPrimeNoTable

Windows (msvc)

View Summary

Windows msvc summary Windows msvc IsPrime Windows msvc IsPrimeNoTable

Windows (clang-cl)

View Summary

Windows clang-cl summary Windows clang-cl IsPrime Windows clang-cl IsPrimeNoTable

Releases

  • 2026/02/25 ver 1.3.3
    • Update copyright year to 2026
    • Add -O3 -march=native to the compilation flags during benchmarking and testing
  • 2026/01/04 ver 1.3.2
    • Improve performance
  • 2025/12/24 ver 1.3.1
    • Improve performance and reduce binary size for cppr::IsPrime
  • 2025/12/21 ver 1.3.0
    • Add CPPR_HAS_CONSTEXPR_IS_PRIME
    • Support clang-cl
    • Accelerating Compile-Time Computation
    • Improved compatibility
  • 2025/03/10 ver 1.2.11
    • Change the name on the license
    • Change Multiprication Algorithm
    • Replace __uint128_t with unsigned __int128
  • 2025/01/05 ver 1.2.10
    • Change the condition of constexpr
  • 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::IsPrime into cppr::IsPrime and cppr::IsPrimeNoTable
  • 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::IsPrime with a table
  • 2024/12/18 ver 1.0.0
    • Add cppr::IsPrime

References

About

πŸš€ An efficient C++ implementation of a primality test

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors