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?

エラトステネスの篩で素数を出力するpythonプログラム

Posted at

エラトステネスの篩で素数を出力するpythonプログラムです。
chmod +x sieve.py
./sieve.py <n>として実行してください。

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

def sieve(n):
  tab=[_ for _ in range(2,n+1)][::-1]
  l=[]
  while (tab):
    l+=[v:=tab.pop()]
    tab=[_ for _ in tab if _ % v ]
  return l

if __name__ == "__main__":
    if (len(sys.argv)!=2):
        print("Invalid argument.")
        print(f"Usage: {sys.argv[0]} <number>")
        exit()
    else:
        n=int(sys.argv[1])
    primes=sieve(n)
    print (f"{primes}\nNumber of primes is {len(primes)}.\n")
0
0
5

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?