A more efficient idea goes like this:
You start with list:
[0,1,2,3,4,5,6,7,8,9,10]
You want to set every element that is not prime to 0, and keep the primes.
Set 0, and 1 to zero, because they are not primes. From now on you need to do these two
steps:
1) Find the the smallest prime you have not considered yet, let's call it n
2) Set every nth element to 0 (but not n), since they are multiples of n
For example: after you set 0 and 1 to 0s:
[0,0,2,3,4,5,6,7,8,9,10]
The smallest prime you have not considered is 2, so you set every second element to zero (but not 2):
[0,0,2,3,0,5,0,7,0,9,0]
The smallest prime that you have not considered is 3, so you set every third element to zero (but not 3), and so on...:
[0,0,2,3,0,5,0,7,0,0,0]
Also notice, that you don't have to do this for every prime, once the primes have reached sqrt(limit) you can stop, because you know that all non-primes have been set to zeros.
For example, square root of 10(limit in this case) is 3.162, this means that we don't have to do anything when we get to 5 and we are done at that point. But why is that? We use each prime to set its multiples to zeros because those multiples are not primes; however, since 5 is larger than square root of 10, any multiple of 5 has to be a multiple of a number smaller than 5, and hence already has been set to 0.
Let's say that our initial range was from up to 20. Square root of 20 is less than 5 so we don't need to check for 5 because all multiples of 5: 5 * 2 = 10, 5 * 3 = 15, 5 * 2 * 2 = 20 are multiples of smaller primes, and we already set them to 0.