sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

素数の個数

Math/Prime_Counting.cpp · ソース · 文書 現行ACあり Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

整数以下の素数の個数を数えます。ふるいの表に必要な領域は平方根に比例します。

assert(sirkit::count_primes(100) == 25);

count_primes

std::uint64_t count_primes(std::uint64_t n);

$n$ 以下の素数の個数を返します。$n<2$ なら0です。$O(n^{3/4})$ 時間・$O(\sqrt n)$ 領域です。

使用例 / Verify

ソースのAPI注記
count_primes(n): n以下の素数の個数。n<2なら0。
異なる商 floor(n/i) をふるい、O(n^(3/4))時間・O(√n)領域。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Counting Primes counting_primes.cpp ✓ 現行AC AC

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/prime_counting.md
//
// API一覧
// count_primes(n): n以下の素数の個数。n<2なら0。
// 異なる商 floor(n/i) をふるい、O(n^(3/4))時間・O(√n)領域。

// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <vector>

// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_COUNT_PRIMES_DEFINED
#define SIRKIT_COUNT_PRIMES_DEFINED

namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
inline std::size_t prime_count_table_size(std::uint64_t root, std::size_t capacity)
{
    if (root >= capacity)
    {
        throw std::length_error("count_primes: table too large");
    }
    return static_cast<std::size_t>(root) + 1;
}
} // namespace detail

inline std::uint64_t count_primes(std::uint64_t n)
{
    if (n < 2)
    {
        return 0;
    }
    // 平方根の上界から整数Newton法を始める。
    auto root = std::uint64_t{1} << ((std::bit_width(n) + 1) / 2);
    for (;;)
    {
        const auto next = (root + n / root) / 2;
        if (next >= root)
        {
            break;
        }
        root = next;
    }
    const auto capacity = detail::prime_count_table_size(root, std::vector<std::uint64_t>{}.max_size());
    std::vector<std::uint64_t> small(capacity), large(capacity);
    for (std::uint64_t i = 1; i <= root; ++i)
    {
        small[i] = i - 1;
        large[i] = n / i - 1;
    }
    for (std::uint64_t p = 2; p <= root; ++p)
    {
        const auto below = small[p - 1];
        if (small[p] == below)
        {
            continue;
        }
        // floor(floor(n/i)/p) = floor(n/(i*p))。被参照項を更新前に読む。
        const auto square = p * p;
        const auto last = std::min(root, n / square);
        for (std::uint64_t i = 1; i <= last; ++i)
        {
            const auto index = i * p;
            const auto source = index <= root ? large[index] : small[n / index];
            large[i] -= source - below;
        }
        for (auto i = root; i >= square; --i)
        {
            small[i] -= small[i / p] - below;
        }
    }
    return large[1];
}

#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page