import java.io.BufferedReader;
import java.io.InputStreamReader;
class test4 {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int N = 1000000000;
// initially assume all integers are prime
boolean[] isPrime = new boolean[N+1];
for (int i = 2; i <= N; i++) {
isPrime[i] = true;
}
// mark non-primes <= N using Sieve of Eratosthenes
for (int i = 2; i*i <= N; i++) {
// if i is prime, then mark multiples of i as nonprime
// suffices to consider mutiples i, i+1, ..., N/i
if (isPrime[i]) {
for (int j = i; i*j <= N; j++) {
isPrime[i*j] = false;
}
}
}
isPrime[1] = false;
while(t-->0)
{
String str[];
str=br.readLine().split(" ");
int m=Integer.parseInt(str[0]);
int n=Integer.parseInt(str[1]);
for(int i=m;i<=n;i++)
{
if(i%2==0&&i!=2)
{
continue;
}
if(isPrime[i])
{
System.out.println(i);
}
}
}
}
}
i am getting the following error
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
in my net beans
and runtime error in code chef
on this line :boolean[] isPrime = new boolean[N+1];
how can i remove it or do i have to use different algorithm ??