I am trying to come up with an algorithm that can find all primes from a starting to an ending number, without starting at 2 and using the Sieve of Eratosthenes to calculate all primes to the ending number and cutting out the interval I want. And not bruteforcing every number in the interval.
I modified an existing notation of the Sieve of Eratosthenes in python. This is what I got so far:
def m(n: int, l: list):
for i, v in enumerate(l):
if v % n == 0 and v != n and v != False:
break
for v in range(i, len(l), n):
l[v] = False
return l
This function can set all common multiplies of a number n in the array l to false, using the same principle as the Sieve of Eratosthenes when marking non-prime numbers. This is how the function is getting called:
l = list(range(10, 20))
for i in range(2, 20):
m(i, l)
print(l)
In this configuration, the program calculates all primes from 10 to (excluded) 20.
The output looks like this:
[False, 11, False, 13, False, False, False, 17, False, False]
But it should look like this:
[False, 11, False, 13, False, False, False, 17, False, 19]
It seems like the last prime number of the array is getting ignored. When I tried to calculate from 10 to (excluded) 21 it detected 19 as a prime number.
What did I do wrong?