有人可以解释这个实现如何工作?还有,它会变得更好吗?怎么样?

use*_*033 3 java algorithm

public class Main { 
    public static void main(String args []){ 
        long numberOfPrimes = 0; //Initialises variable numberOfPrimes to 0 (same for all other variables)
        int number = 1; 
        int maxLimit = 10000000; 
        boolean[] sieve = new boolean[maxLimit]; //creates new boolean array called sieve and allocates space on the
                                                //stack for this array which has maxLimit spaces in it 
        for ( int i = 2; i < maxLimit; i++ ) { //for statement cycling from 2 to 10000000, does not execute the rest
                                              //of the block if the boolean value in the array is true 
            if ( sieve[i] == true ) continue; 

            numberOfPrimes++; //otherwise it increments the number of prime numbers found

            if ( numberOfPrimes == 10001 ) {  //if 10001st prime number is found, break from loop
                number = i; 
                break; 
            } 

            for ( int j = i+i; j < maxLimit; j += i ) //do not understand the point of this loop logically
                sieve[j] = true;                      //testing if the value in the array is true again?
        } 
        System.out.println("10001st prime: "+ number); 
    } 
    }
Run Code Online (Sandbox Code Playgroud)

我真的不明白这个程序发生了什么,希望有人可以向我解释一下吗?我已经评论了导致我麻烦的具体线路/我理解的线路.非常感谢您的帮助!:)

Nik*_*bak 7

熟悉Eratosthenes的Sieve算法.维基百科甚至还有GIF动画演示了这个过程.而你的代码只是一个简单的实现.