Prime Visits Question:I am not able to find the error in my code. One test case is showing TLE and other is showing wrong. I am not able to understand the solution as well. Please help

import java.util.*;
public class Main {
public static Scanner scn = new Scanner(System.in);
public static void main(String args[]) {
int n = scn.nextInt();

	for(int i=0; i<n; i++)
	{
		int a = scn.nextInt();
		int b = scn.nextInt();
		nOfPrime(a,b);
	}
}

public static void nOfPrime(int a, int b)
{
	int count=0;
	
	for(int i=a; i<=b; i++)
	{
		boolean flag = true;
		if(i==1)
		flag = false;

		for(int j=2; j*j<=i; j++)
		{
			if(i%j==0)
			{
				flag = false;
				break;
			}
		}
		if(flag==true)
		count++;
	}
	System.out.println(count);
}

}

To overcome TLE, use Sieve. And wrong answer is probably due to some corner cases. Hard code them.

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.