Finding prime numbers with the Sieve of Eratosthenes (Originally: Is there a better way to prepare this array?)

Your method of finding primes, by comparing every single element of the array with every possible factor is hideously inefficient. You can improve it immensely by doing a Sieve of Eratosthenes over the entire array at once. Besides doing far fewer comparisons, it also uses addition rather than division. Division is way slower.

Leave a Comment