Can someone explain how this implementation works? Also, can this be done better? How?

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); 
    } 
    }

I really don’t understand what is going on in this program, and was hoping that someone could explain this to me? I commented on specific lines that are causing me problems / that I understand what needs to be done. Thank you very much for your help!:)

+3
source share
6 answers

Yes, this is your main implementation of the Eratosthenes sieve. There are several ways you can improve it, but first let go of the basic principle.

What you are doing is creating an array of booleans. The INDEX in the array represents the number we are testing to see if it is prime or not.

, , . -, " , 1 ".

for ( int i = 2; i < maxLimit; i++ )

INDEX 2 ( 3), 1 2 . ( , 1 ).

if ( sieve[i] == true ) continue;

, .

numberOfPrimes++;

        if ( numberOfPrimes == 10001 ) {
            number = i; 
            break; 
        }

INDEX, , , , , . , , , , 10001 , . , , .

for ( int j = i+i; j < maxLimit; j += i )
            sieve[j] = true;

. , , 1. , , , . , for, 3. [2] ( ), ( 3 IS PRIME!). 3 . 3 . : [5] = true; [8] = true... .

, , , , , , , , . , , false, .

, , . , !

+2
+7
for ( int j = i+i; j < maxLimit; j += i ) //dont understand the point of this loop logically
                sieve[j] = true;          //testing if the value in the array is true again ?

, . , i true. i 2, 4, 6, 8... true. i 3, 6, 9, 12... true ..

if,

if ( sieve[i] == true ) continue; 

... , true, .

+1

, - - . , , ?

& ; 9999998 & ;

i = 2
sieve[2] - false, .
numberOfPrimes = 1 , ,
2 true sieve[].

& ; 9999997 & ;

i = 3
sieve[3] false, .
numberOfPrimes = 2 , , 3 true sieve[].

& ; 9999996 & ;

i = 4
sieve[4] - true ( ). .

..... Termina.

+1

, , , .

maxLimit. , , , , .

0

1 .

2nd true , . , , true, 3, 4 .. , false, - .

0

Source: https://habr.com/ru/post/1771349/


All Articles