1
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プログラム

Last updated at Posted at 2023-11-26

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

sieve.py
#!/usr/bin/env 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")
1
0
1

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
1
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?