Prime Visits Question

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int p=sc.nextInt();
for(int i=0;i<p;i++){
int m=sc.nextInt();
int n=sc.nextInt();
prime(m,n);
}

}
static void prime(int low,int high){
	int c=0;
while (low < high) {
        boolean flag = false;

        for(int i = 2; i <= low/2; ++i) {
            // condition for nonprime number
            if(low % i == 0) {
                flag = true;
                break;
            }
        }

        if (!flag && low != 0 && low != 1){
			c++;
		}

        ++low;
}
System.out.println(c);

}
}

Unable to pass all the test cases due to brute force approach. what other approach can be used?

Use Sieve of Eratosthenes to store all the prime number upto 10^6. Then simply print the xth prime number.

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.