-4

It's a problem on Sphere Online Judge (SPOJ) for prime generator.

Input: number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n.

Output: For every test case print all prime numbers p such that m <= p <= n.

I have made prime[0] and prime1 as -1.

    int range[][] = new int[t][2];
    for (int i = 0; i < t; i++)     //take t ranges
    {
        for(int j = 0; j < 2; j++)
        {
            range[i][j] = sc.nextInt();
        }

    }
    for(int i = 0; i < t; i++)                       
    {
        int prime[] = new int[range[i][1]+1];
        for(int k = 2; k <= range[i][1]; k++)
        {
            prime[k] = k;
        }
        prime[0] = -1;
        prime[1] = -1;
        for(int k = 2; k <= range[i][1]; k++)
        {
            for(int m = k + 1; m < range[i][1]; m++)
            {
                int x = prime[k];
                if(prime[m] % x == 0)
                {
                    prime[m] = 0;
                }
            }
        }
    }

I tried solving it by the sieve of eratosthenes. The output is: Exception in thread "main" java.lang.ArithmeticException: / by zero

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your full source code you have as a [mcve], which can be compiled and tested by others. – Progman Oct 06 '19 at 17:34
  • 2
    Also check https://stackoverflow.com/questions/26226863/java-lang-arithmeticexception-by-zero – Progman Oct 06 '19 at 17:34
  • 1
    Possible duplicate of [java.lang.ArithmeticException: / by zero](https://stackoverflow.com/questions/26226863/java-lang-arithmeticexception-by-zero) – Richard Chambers Oct 06 '19 at 17:58
  • Possible duplicate of [ArithmeticException divide by zero occasionally](https://stackoverflow.com/questions/28997474/arithmeticexception-divide-by-zero-occasionally) – Michiel Leegwater Oct 06 '19 at 18:17

1 Answers1

0

You are seeing a divide by zero exception because a statement in your program is attempting to divide a value by a value of zero and this is not allowed in Java or any other programming language that I have knowledge of.

There are two things you can do, (1) check the value of the divisor and if it is zero then do not do the divide or (2) catch the exception using a try/catch.

See Java if vs. try/catch overhead as well as Using try-catch java

Looking at your code, I am not sure where the divide by zero is happening though it may be this area where you are using the modulus operator (%) to check if the remainder of a division is zero:

for(int m=k+1; m<range[i][1]; m++)
{
    int x = prime[k];
    if(prime[m] % x == 0)  // use modulus operator to check the remainder
    {
        prime[m] = 0;
    }
}

You may want to do something like:

for(int m=k+1; m<range[i][1]; m++)
{
    int x = prime[k];
    if(x == 0 || prime[m] % x == 0)  // use modulus operator to check the remainder
    {
        prime[m] = 0;
    }
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • Note: "this is not allowed in Java" - it is allowed for `double` - [JLS 15.17.2. Division Operator /](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.17.2): "The result of a floating-point division ...: Division of a nonzero finite value by a zero results in a signed infinity" – user85421 Oct 06 '19 at 18:05
  • @CarlosHeuberger thank you for the clarification as to a special case. This is `int` however the rules on floating point you referenced are new to me. I don't do much with `double`. What is signed infinity? – Richard Chambers Oct 06 '19 at 18:12
  • `Double.POSITIVE_INFINITY` or `Double.NEGATIVE_INFINITY`, some *bit-patterns* that represent positive infinity or negative infinity, similar to `Double.NaN` that represents Not-a-Number (e.g `0.0 / 0.0`) [also valid for `Float`] – user85421 Oct 06 '19 at 18:19