4 / 4
Oct 2010

Hi,
I'm alway getting an NZEC Error by submitting my solution for PRIME1 in Java.
My approach is to save all testcases into an array first and then to determine the maximum value for the upper bound.
Then I create the Erathosthenes sieve from 0 to the maximum bound and at last I run through all the testcases in the original order to print out all the primes between the given bounds.

Here's the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
	static int cases;
	static int [] [] eingabe;
	static StringTokenizer st;
	static int minBound;
	static int maxBound;
	static boolean [] sieve;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		cases = Integer.valueOf(input.readLine());
		eingabe = new int [cases] [2];
		for(int i = 0; i < cases; i++){
			st = new StringTokenizer(input.readLine());
			eingabe [i] [0] = Integer.valueOf(st.nextToken());
			eingabe [i] [1] = Integer.valueOf(st.nextToken());
		}
		maxBound = getMaxBound();
		createSieve();		
		/* Testausgabe
		for(int i = 0; i < sieve.length; i++){
			System.out.println("[" + i + "] = " + sieve[i]);
		}
		*/
		for(int i = 0; i < cases; i++){
			minBound = eingabe [i] [0];
			maxBound = eingabe [i] [1];
			for(int j = minBound; j <= maxBound; j++){
				if(sieve[j])
					System.out.println(j);
			}
			System.out.println();
		}
	}
	static void createSieve(){
		sieve = new boolean [maxBound+1];
		int faktor;
		for(int i = 2; i < sieve.length; i++){
			sieve[i] = true;
		}
		for(int i = 2; i < sieve.length; i++){
			if(sieve[i]){
				faktor = i;
				for(int j = 2; j*faktor <= maxBound; j++){
					sieve[faktor*j] = false;
				}
			}
		}
	}
	static int getMinBound(){
		int result = eingabe [0] [0];
		for(int i = 1; i < cases; i++){
			if(eingabe [i] [0] < result)
				result = eingabe [i] [0];
		}
		return result;
	}
	static int getMaxBound(){
		int result = eingabe [0] [1];
		for(int i = 1; i < cases; i++){
			if(eingabe [i] [1] > result)
				result = eingabe [i] [1];
		}
		return result;
	}
}

I would be thankful for any hint. On other problems I did also get NZEC errors quite often, what is causing them in Java? The FAQ says programs crashed or uncaught exception, but that could be really much causes and the judge doesn't give me a hint on what part the error occured.

Greets,

Auron

  • created

    Dec '09
  • last reply

    Oct '10
  • 3

    replies

  • 265

    views

  • 3

    users

  • 1

    link

The fact you have to ask shows you haven't tested your code at all. Read the input section, and try testing your code on some useful test cases.

Okay, I did some more testing and found out, that there are no problems with rather small numbers, but if I use values that are near to the given maximum value the Java VM runs out of memory.
I guess my problem must be then, that I cannot create an Array from 0 to the highest bound given in the testcase, because it needs to much space.
I'll think of a change of my algortihmn then...

10 months later

Suggested Topics

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