Challenges- Array problem prime visits

from starting to end i did not understand the solution
can you plzz understand me the code line by line

public static void primeVisit() {

    Scanner scn = new Scanner(System.in);
    int t = scn.nextInt();
    while (t-- > 0) {
        int a = scn.nextInt(), b = scn.nextInt();

        int[] primes = new int[b + 1];
        primes[0] = 0;
        primes[1] = 0;

        for (int i = 2; i <= b; i++) {
            if ((i & 1) == 0 || i < a) {
                primes[i] = 0;
            } else {
                primes[i] = 1;
            }
        }
        if (2 >= a) {
            primes[2] = 1;
        }
        for (int i = 3; i <= b; i += 2) {

            int j = 2;
            while (i * j <= b) {
                primes[i * j] = 0;
                j++;
            }
        }

        int count = 0;
        for (int i = 0; i <= b; i++) {
            if (primes[i] == 1) {
                count++;
            }
        }

        System.out.println(count);
    }
}

public static boolean isPrime(int num) {
    if (num <= 0)
        return false;
    if (num == 1)
        return false;
    if (num == 2)
        return true;
    if ((num & 1) == 0)
        return false;

    boolean isPrime = true;
    for (int i = 3; i * i <= num; i += 2) {
        if (num % i == 0) {
            isPrime = false;
            break;
        }
    }
    return isPrime;
}

@mayanktiwari6957,

You are supposed to use the prime seive optimisation technique. Also make sure that you call the primeseive function that you will make before the test cases only to prevent TLE.

Precompute the prime numbers using sieve technique.

In the sieve technique where you create a boolean array of 1000005 size.

And start from the first prime number i.e. 2. Take its square(4) and increment it by the number you took (i.e. 4,6,8,10 and so on). Mark all these as false since they cannot be primes.

Now take the next unmarked number, 3. And mark 9,12,15 and so as false. Similarly do it for 4. 16,20,24 and so on as false.

When you finish the loop, count the number of primes within the range.

Now the code given in the editorial is a little different. First we mark all the even numbers between a and b as non prime, except 2. After that we start from 3 and mark all the numbers which are multiples of 3 (6,9,12 …) as non prime. After that we just count prime numbers between a and b

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.