cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 素数判定 (primality test)
(include/emthrm/math/is_prime.hpp)

時間計算量

$O(\sqrt{N})$

仕様

名前 戻り値
bool is_prime(const long long n); $n$ は素数であるか。

参考文献

TODO

Submissons

https://onlinejudge.u-aizu.ac.jp/solutions/problem/ALDS1_1_C/review/4088317/emthrm/C++14

Verified with

Code

#ifndef EMTHRM_MATH_IS_PRIME_HPP_
#define EMTHRM_MATH_IS_PRIME_HPP_

namespace emthrm {

bool is_prime(const long long n) {
  if (n <= 1) [[unlikely]] return false;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i == 0) return false;
  }
  return true;
}

}  // namespace emthrm

#endif  // EMTHRM_MATH_IS_PRIME_HPP_
#line 1 "include/emthrm/math/is_prime.hpp"



namespace emthrm {

bool is_prime(const long long n) {
  if (n <= 1) [[unlikely]] return false;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i == 0) return false;
  }
  return true;
}

}  // namespace emthrm
Back to top page