TESTCASE # 1 : correct (Time: 0.04 s) TESTCASE # 2 : wrong-answer (Time: 0.04 s) TESTCASE # 3 : timelimit (Time: 5.98 s)

Where am I making a mistake?

Code:

import java.util.*;
public class Main {
public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);
    int n = 0;
    if(sc.hasNextInt())
        n = sc.nextInt();
    
    long a = 0;
    long b = 0;

    for(int i = 0; i < n; i++) {
        if(sc.hasNextLong()) {
            a = sc.nextLong();
            b = sc.nextLong();
        }
        primeVisits(a, b);
    }
}

public static void primeVisits(long a, long b) {
    long count = 0;
    for(long i = a; i <= b; i++) {
        if(i == 1)
            i++;
        if(isPrime(i))
            count++;
    }
    System.out.println(count);
}

public static boolean isPrime(long x) {
    boolean flag = true;

    for(long i = 2; i <= x - 1; i++) {
        if(x % i == 0) {
            flag = false;
        }
        if(flag == false)
            break;
    }
    return flag;
}

}

Hi Navneet,
In this question the sieve method will be used to find the prime numbers and this method ou will study in time complexity section.

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.