2019-10-22 17:13:48 +00:00
|
|
|
"""
|
2019-10-21 18:10:19 +00:00
|
|
|
Sieve of Eratosthenes
|
|
|
|
|
|
|
|
Input : n =10
|
|
|
|
Output : 2 3 5 7
|
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
Input : n = 20
|
2019-10-21 18:10:19 +00:00
|
|
|
Output: 2 3 5 7 11 13 17 19
|
|
|
|
|
2020-01-18 12:24:33 +00:00
|
|
|
you can read in detail about this at
|
2019-10-21 18:10:19 +00:00
|
|
|
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
2019-10-22 17:13:48 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
|
|
|
|
def prime_sieve_eratosthenes(num):
|
|
|
|
"""
|
2020-01-18 12:24:33 +00:00
|
|
|
print the prime numbers up to n
|
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
>>> prime_sieve_eratosthenes(10)
|
|
|
|
2 3 5 7
|
|
|
|
>>> prime_sieve_eratosthenes(20)
|
|
|
|
2 3 5 7 11 13 17 19
|
|
|
|
"""
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
primes = [True for i in range(num + 1)]
|
|
|
|
p = 2
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
while p * p <= num:
|
2020-01-18 12:24:33 +00:00
|
|
|
if primes[p]:
|
2019-10-22 17:13:48 +00:00
|
|
|
for i in range(p * p, num + 1, p):
|
2019-10-21 18:10:19 +00:00
|
|
|
primes[i] = False
|
2019-10-22 17:13:48 +00:00
|
|
|
p += 1
|
2019-10-21 18:10:19 +00:00
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
for prime in range(2, num + 1):
|
2019-10-21 18:10:19 +00:00
|
|
|
if primes[prime]:
|
|
|
|
print(prime, end=" ")
|
|
|
|
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
num = int(input())
|
2019-10-22 17:13:48 +00:00
|
|
|
|
2019-10-21 18:10:19 +00:00
|
|
|
prime_sieve_eratosthenes(num)
|