cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 数学/素数判定
(test/math/is_prime.test.cpp)

Depends on

Code

/*
 * @title 数学/素数判定
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C
 */

#include <iostream>

#include "emthrm/math/is_prime.hpp"

int main() {
  int n;
  std::cin >> n;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int num;
    std::cin >> num;
    if (emthrm::is_prime(num)) ++ans;
  }
  std::cout << ans << '\n';
  return 0;
}
#line 1 "test/math/is_prime.test.cpp"
/*
 * @title 数学/素数判定
 *
 * verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_C
 */

#include <iostream>

#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


#line 10 "test/math/is_prime.test.cpp"

int main() {
  int n;
  std::cin >> n;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int num;
    std::cin >> num;
    if (emthrm::is_prime(num)) ++ans;
  }
  std::cout << ans << '\n';
  return 0;
}
Back to top page