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?

More than 1 year has passed since last update.

エラトステネスの篩

Last updated at Posted at 2024-05-29

エラトステネスの篩とは

素数を見つけ出すアルゴリズム。
「その数の平方根より小さい素数の倍数を消せば、残った数が素数である」という考え方に従って素数を探す。

フローチャート

100以下の素数を全て求める場合、以下のアルゴリズムとなる。

素数かどうか確かめたい数を代入する配列を、arrayとする。
iarrayの添字。
kは倍数を取り除く素数。(kが2なら2の倍数を取り除き、kが3なら3の倍数を取り除く)

変数arrayに0~100までの全ての添字に値1が対応した状態でスタートし、素数でない添字には0を代入していく。
素数でない添字に0を代入する処理が終わった段階で要素が1の添字が素数となる。

image.png

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?