Sieve of Eratosthenes Issue Java

前端 未结 1 1727
长发绾君心
长发绾君心 2021-01-15 00:03

I\'ve got an issue with an assignment that I have requiring the use of arrays. I need to create the Sieve of Eratosthenes algorithm and print out all the prime numbers. I\'m

1条回答
  •  长发绾君心
    2021-01-15 00:42

    Here:

            for(int n = 2; y < 1000; n++){
            y = n * x;
            numbers[y] = false;
            }
    

    you first check that y < 1000, and then intialize and use it. This is the wrong way around.

    Also, you can get away with running the above loop only when x is prime. This won't affect correctness, but should make your code much faster.

    0 讨论(0)
提交回复
热议问题