What is the best practice to output a list of prime numbers between two numbers?How can I achieve a better running time? This is a solution to a SPOJ problem which is getting Time Limit Exceeded.import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;class SPOJ2 {        public static void main(String[] args) {                try{                        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));                        int times = Integer.parseInt(reader.readLine().toString());                        int temp=times-1;                        String[] input_string = new String[times];                        int[] start_number = new int[times];                        int[] end_number = new int[times];                        int min_start_number=0, max_end_number=0;                        while(temp>=0){                                input_string[temp] = reader.readLine().toString();                                --temp;                        }                        temp=times-1;                        while(temp>=0){                                String[] array_string = input_string[temp].split(\\s);                                start_number[temp] = Integer.parseInt(array_string[0]);                                end_number[temp] = Integer.parseInt(array_string[1]);                                if(min_start_number == 0 || min_start_number > start_number[temp]){                                        min_start_number = start_number[temp];                                }                                if(max_end_number < end_number[temp]){                                        max_end_number = end_number[temp];                                }                                if(start_number[temp] > end_number[temp]){                                        end_number[temp] = start_number[temp]+1;                                }                                --temp;                        }                        Prime prime_object = new Prime();                        List<Integer> output_list = prime_object.primeBetween(min_start_number, max_end_number);                        temp = times-1;                        while(temp>=0){                                for(int count=0; count<output_list.size(); count++){                                        if(output_list.get(count) >= start_number[temp] && output_list.get(count) <= end_number[temp]){                                                System.out.println(output_list.get(count));                                        }                                }                                --temp;                                if(temp != times) System.out.println();                        }                }catch(Exception e){                        return;                }        }}class Prime {        public List<Integer> primeBetween(int start_number, int end_number){                int last_check_number = (int)Math.sqrt(end_number);                int start_check_number = (int)Math.sqrt(start_number);                List<Integer> primes_list = new ArrayList<Integer>();                for(int count=2; count<=end_number; count++){                        primes_list.add(count);                }                for(int outer_i=0; (primes_list.get(outer_i)<=start_check_number || (primes_list.get(outer_i)>start_check_number && primes_list.get(outer_i)<last_check_number)); outer_i++){                        for(int inner_i=outer_i; primes_list.get(inner_i)<last_check_number; inner_i++){                                int check_number = primes_list.get(inner_i);                                for(int temp=2; (temp*check_number)<=end_number; temp++){                                        primes_list.remove(new Integer(temp*check_number));                                }                        }                }                return primes_list;        }}
Outputting prime numbers between two numbers
java;programming challenge;primes;time limit exceeded