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

前端 未结 13 1616
攒了一身酷
攒了一身酷 2020-12-02 23:38

Note: Version 2, below, uses the Sieve of Eratosthenes. There are several answers that helped with what I originally asked. I have chosen the Sieve of Era

13条回答
  •  抹茶落季
    2020-12-03 00:22

    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.

提交回复
热议问题