Sieve of Eratosthenes
This is an implementation of the Sieve of Eratosthenes.
You can find the full description of the algorithm on its Wikipedia page here.
Code
n = 120
consecutive_int = [True for _ in range(2, n + 1)]
def mark_multiples(ci, p):
for i in range(p * p, len(ci) + 2, p):
ci[i - 2] = False
return ci
def get_next_prime_notmarked(ci, p):
for i in range(p + 1, len(ci) + 2):
if ci[i - 2]:
return i
return - 1
next_prime = 2
while True:
consecutive_int = mark_multiples(consecutive_int, next_prime)
next_prime = get_next_prime_notmarked(consecutive_int, next_prime)
if next_prime == -1:
break
def convert_arr_nums(consecutive_int):
num = ""
for i in range(len(consecutive_int)):
if consecutive_int[i]:
num += str(i + 2) + " "
return num
print(convert_arr_nums(consecutive_int))