2019-07-10 20:09:24 +00:00
|
|
|
"""Sieve of Eratosthones."""
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
import math
|
2019-07-10 20:09:24 +00:00
|
|
|
|
|
|
|
N = int(input("Enter n: "))
|
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
def sieve(n):
|
2019-07-10 20:09:24 +00:00
|
|
|
"""Sieve of Eratosthones."""
|
|
|
|
l = [True] * (n + 1)
|
2018-10-19 12:48:28 +00:00
|
|
|
prime = []
|
|
|
|
start = 2
|
2019-07-10 20:09:24 +00:00
|
|
|
end = int(math.sqrt(n))
|
|
|
|
while start <= end:
|
|
|
|
if l[start] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
prime.append(start)
|
2019-07-10 20:09:24 +00:00
|
|
|
for i in range(start * start, n + 1, start):
|
|
|
|
if l[i] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
l[i] = False
|
|
|
|
start += 1
|
2019-07-10 20:09:24 +00:00
|
|
|
|
|
|
|
for j in range(end + 1, n + 1):
|
|
|
|
if l[j] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
prime.append(j)
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
return prime
|
|
|
|
|
2019-07-10 20:09:24 +00:00
|
|
|
|
|
|
|
print(sieve(N))
|