serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 素数判定
(library/number/prime/prime_test.hpp)

素数判定

できること

計算量

$O(\sqrt N)$

使い方

bool is_prime = prime_test(N);

Verified with

Code

#pragma once
bool prime_test(int N) {
    if (N == 2) return true;
    if (N == 1 or N % 2 == 0) return false;
    for (int i = 3; i * i <= N; i += 2) {
        if (N % i == 0) return false;
    }
    return true;
}
#line 2 "library/number/prime/prime_test.hpp"
bool prime_test(int N) {
    if (N == 2) return true;
    if (N == 1 or N % 2 == 0) return false;
    for (int i = 3; i * i <= N; i += 2) {
        if (N % i == 0) return false;
    }
    return true;
}
Back to top page