Sieve_of_Erathosthenes

Write an algorithm (function) that uses the sieve of Erathosthenes to calculate/find all prime numbers in the range of the parameter (int).

Information on the sieve of Erathosthenes (famous Greek mathematician and geographer): https://byjus.com/maths/sieve-of-eratosthenes/











scroll down to see my code...







def print_primes_sieveErathosthenes(n: int):
import math

prime_num = [True for _ in range(n + 1)]
prime_num[0] = prime_num[1] = False
i = 2
while i <= (math.sqrt(n) + 1):
if prime_num[i]:
for a in range(i * 2, n + 1, i):
prime_num[a] = False
i += 1
for p in range(n + 1):
if prime_num[p]:
print(p, end=', ')

Comments