I studied Sieve of Eratosthenes from GFG, Talking about case 1, it works fine on my local ide but still unable to submit. Here’s my code:
public class prime_visits {
static int[] primes = new int[10001];
static boolean[] isPrime = new boolean[10001];
public static void main(String[] args) {
try {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int a = scn.nextInt();
int b = scn.nextInt();
int temp_ans = give_all_primes(a, b);
ans[i] = temp_ans;
}
for (int i = 0; i < n; i++) {
System.out.println(ans[i]);
}
} catch (Exception e) {
;
}
}
public static int give_all_primes(int a, int b) {
return primes[b] - primes[a-1];
}
public static void build_prefix() {
Arrays.fill(isPrime, true);
for (int i = 2; i * i < isPrime.length; i++) {
if (isPrime[i] == true) {
for (int j = 2 * i; j < isPrime.length; j = j + i)
isPrime[j] = false;
}
}
for (int i = 0; i < primes.length; i++) {
if (i == 0 || i == 1)
primes[i] = 0;
else {
primes[i] = primes[i - 1];
if (isPrime[i] == true)
primes[i]++;
}
}
}
}