Currently, the code will generate a range of natural numbers from 1 to N and store it in a list. Then the program will iterate through that list, marking each multiple of a prime as 0 and storing each prime in a secondary list.
This is a translation of a TI-Basic program, shown here, so the same description still applies.
Here's an animation:
One of the main optimizations that I want to see implemented would be the actual removal of prime multiples from the list instead of simply setting them to 0. As it is currently written, this would be a challenge to perform without adding in another if
statement. Any suggestions would be helpful.
def sieve(end):
prime_list = []
sieve_list = list(range(end+1))
for each_number in range(2,end+1):
if sieve_list[each_number]:
prime_list.append(each_number)
for every_multiple_of_the_prime in range(each_number*2, end+1, each_number):
sieve_list[every_multiple_of_the_prime] = 0
return prime_list
Here's a link to PythonTutor, which will visualize the code in operation.