21 / 23
Feb 2016
12 days later

Can anyone help me with this?
Tried a few things so as to stop losing time. Still getting WA.
My code reads,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
	public static ArrayList<Integer> primes;

public static void main(String[] args) throws NumberFormatException, IOException {
	generatePrimes();
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int t = Integer.parseInt(br.readLine());
	int low,high;
	while (t-- > 0) {
		String[] w = br.readLine().split(" ");
		low=Integer.parseInt(w[0]);
		high=Integer.parseInt(w[1]);
		if(low<2){
			low=2;
		}
		if(high<2){
			high=2;
		}
		printPrimes(low,high);
		System.out.print("\n");
	}
}

private static void generatePrimes() {
	boolean[] dataset= new boolean[100001];
	primes=new ArrayList<>();
	int i, j;
	for(i=2;i<100001;i++){//Set all values to true
		dataset[i]= true;
	}
	for(i=2; i<100001; i++){
		if(dataset[i]){
			for(j=2*i; j< 100001; j=j+i){
				dataset[j]= false;
			}
		}
	}
	for(i=2; i<100001; i++){
		if(dataset[i]){
			primes.add(i);
		}
	}
}

private static void printPrimes(int low, int high) {
	for (Integer val : primes){
		if(val>=low && val<=high){
			System.out.println(val.toString());
		}
	}
}

}

I guess there might be an issues regarding pretty printing stuck_out_tongue

How do you generate primes greater than 100001? The constraints of the problem statement "(1 <= m <= n <= 1000000000, n-m<=100000)" only mean that the difference between m and n in not greater than 100000.

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.