Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 40d4000
  • /
  • src
  • /
  • math
  • /
  • prime.cpp
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:8f050e6fd49454ea40ffddcbbacad94d1d791af5
directory badge
swh:1:dir:2685fe8e758ef3e9022e968ae013830f90cd5f51

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
prime.cpp
#include <tdc/math/isqrt.hpp>
#include <tdc/math/prime.hpp>

#include <tdc/util/likely.hpp>

bool tdc::math::is_prime(const uint64_t p) {
    if(p % 2 == 0) {
        return false;
    } else {    
        const uint64_t m = isqrt_ceil(p);

        // first, check against small primes less than sqrt(p)
        size_t j = 2;
        uint64_t i = SMALL_PRIMES[j];
        while(i <= m && j < NUM_SMALL_PRIMES - 1) {
            if((p % i) == 0) return false;
            i = SMALL_PRIMES[++j];
        }

        // afterwards, use folklore algorithm for i less than sqrt(p)
        i = 5ULL + ((i - 5ULL) / 6ULL) * 6ULL;
        while(i <= m) {
            if(((p % i) == 0) || (p % (i+2) == 0)) return false;
            i += 6ULL;
        }
        return true;
    }
}

uint64_t tdc::math::prime_successor(const uint64_t p_) {
    uint64_t p = p_;
    
    if(tdc_unlikely(p == 0)) return 0;
    if(tdc_unlikely(p == 2)) return 2;
    if(p % 2 == 0) ++p; // all primes > 2 are odd

    // linear search - the gap between two primes is hopefully very low
    // in the worst case, because there must be a prime between p and 2p, this takes p steps
    while(!is_prime(p)) p += 2;
    return p;
}

uint64_t tdc::math::prime_predecessor(const uint64_t p_) {
    uint64_t p = p_;
    
    if(tdc_unlikely(p == 0)) return 0;
    if(tdc_unlikely(p == 2)) return 2;
    if(p % 2 == 0) --p; // all primes > 2 are odd

    // linear search - the gap between two primes is hopefully very low
    // in the worst case, because there must be a prime between p and 2p, this takes p steps
    while(!is_prime(p)) p -= 2;
    return p;
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API