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.

エラトステネスのふるいによる素数生成 Python3

Last updated at Posted at 2023-01-09

エラトステネスの篩による素数生成をします。
ファイルに、chmod +x prime5.pyとして実行権を付けて動かして下さい。
$ prime5.py <n>で、nまでの素数を生成します

prime5.py
#!/usr/bin/python3
import sys

def primes(n):
  p = [i for i in range(n + 1)] # 0からnまでの数のうち、
  for i in range(2, int(n**0.5)+1): # 2 から√nまでのうち、
   if p[i] != 0: # 最初の数が見つかったら
      for j in range(2, int(n/i)+1): # 倍数を次々と消してゆく
           p[i*j]=0
  return( [_ for _ in p if _ != 0][1:] ) # リストを整形する

argvs=sys.argv
n =int(argvs[1])
pl=primes(n)
print(pl)
print(len(pl))

0
0
2

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?