0
0

More than 1 year has passed since last update.

試し割り法による素数の生成

Last updated at Posted at 2023-01-07

試し割り法による素数の生成です。
prime2.pyとして実行したら、素数が1行毎、次々と出力されます。
chmod +x prime2.pyとして、実行権限を付けて動かして下さい。

prime2.py
#!/usr/bin/python3
#
#試し割り法による素数の生成
#
def trial_division(t):
  for i in range(2,t):
    if t % i == 0:
      return(0)
  return(1)
 
def nth_prime(n):
  m=2
  primes=[]
  while(1):
    if (trial_division(m)==1):
      primes.append(m)
      if (len(primes)==n):
        return(primes[n-1])
    m=m+1

n=1
while(1):
  print(nth_prime(n))
  n=n+1
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