0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

素数を爆速で探すアルゴリズム[競プロ]

Posted at

コード

エラトステネスの篩 ~ Sieve of Eratosthenes
const int N = 1e6;
vector<bool> isPrime(N+1, true);
isPrime[0] = isPrime[1] = false;
for(int i=2; i<N; ++i) {
    if (isPrime[i]) for (int j = i*i; j <= N; j+=i) isPrime[j] = false;
}

素数篩の解説

「そすうふるい」と読むそうです。
これは素数の原則に基づくものです。
はじめに例外処理をしています。0はdummyで、1は定義上素数ではないので。
そして、その他全ての数が素数だと見做して始まります。

  1. 数字を倍率に含むものはアウト
  2. 重複参照を防ぐ

その意図が込められたforループです。

まとめ

こちらのコードはchatgpt先生に出力してもらいました。
AIでの学習って効率よくて知識欲求が満たされて最高ですよね?
それでは!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?