The code is running for all the cases except for the test case (1,10), where it is printing 1 as a prime no

package questions;

import java.util.Scanner;

public class printPrime{
public static void printprime(int a,int b){
int count=0;

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

public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
printprime(a,b);

//public static void main(String[] args) {
	// TODO Auto-generated method stub

}

}

@vaibhavvishal98,

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.

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.