NZEC Prime number Generation SPOJ- http://www.spoj.com/problems/PRIME1/ - java

I am beginner at coding. I am getting NZEC error on submission of my code for prime number genertaion to spoj. But the code is working perfectly fine in my desktop. Kindly help me. This is what i have coded.
import java.util.*;
import java.lang.*;
import java.io.*;
import static java.lang.Math.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int testcase;
int lower_limit,upper_limit;
int i,j,k;
boolean[] a= new boolean[100001];
Arrays.fill(a, Boolean.TRUE);
Scanner sc1 = new Scanner(System.in);
testcase= sc1.nextInt();
for(;testcase>0;testcase--)
{
lower_limit= sc1.nextInt();
upper_limit= sc1.nextInt();
for(i = 2;i<sqrt(upper_limit);i++)
{
if(a[i]=true)
{
for(j=i;j<=upper_limit;j=j+i)
{
a[j]=false;
}
}
}
for(i=lower_limit;i<upper_limit;i++)
{
if(a[i]==true)
{
System.out.println(i);
}
}
}
}
}

Your array goes out of index for large values of m and n. Try this sample test case on your system where m = 999900000 and n = 1000000000. You can't store these large values as the index of the array. Even m or n = 10^8 will go out of bound index for an array.

Related

Error in test cases for a recursion problem

I am solving a code for competitive programming and my code which I think is correct is throwing an error for some test cases:
The question is to find the total number of digits in an integer using recursion.
My Code:
import java.util.*;
import java.io.*;
import java.lang.*;
class Driver_class
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n =sc.nextInt(); // taking number "n" as input
System.out.println(new Digitcount().countDigits(n)); // prints the count of digits
}
}
}
class Digitcount
{
static int count=0;
public static int countDigits(int n)
{
if(n<1)
return 0;
countDigits(n/10);
count++;
return count;
}
}
The count variable is the total number of digits.
The test case that this code fails:
However, this code passes all the test cases:
class Digitcount
{
static int count=0;
public static int countDigits(int n)
{
if(n<1)
return 0;
return countDigits(n/10)+1;
}
}
I dont see a difference in any of my codes regarding the output value. What is causing the first code to fail the test case?
The difference is in use of a static variable. It will hold the result between tests and thus in any test after the first one it will be incorrect. If you have two calls one after the other like so:
int a = countDigits(605);
int b = countDigits(605);
The result will be a==3, b==6 - the first result is carried over to the second call.

CoderByte says all of my testcases are wrong?

I tried coderbyte and looks something is wrong with it. The first challenge is simply to reverse a string. I did this in java:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
char[] chars = str.toCharArray();
String ret = "";
for (int i = chars.length - 1; i >= 0; i--) {
ret += chars[i];
}
return ret;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
It said, that three test cases had wrong output and there was the correct output. I tried running the code with the specified cases and it printed the same string as correct output for that case. So I tried resubmitting it and it said that only one test case was correct and all other had wrong out put. So I said OK and rewrote my code this way:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
Unfortunately it still says only one test case was successful... Any ideas what happened? Thanks

Big Integer Java

Why is my code printing the output 4 times? The answer is correct but the answer is printed 4 times instead of the desired one time.
import java.util.*;
import java.math.BigInteger;
class THIRTYSEVEN
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
BigInteger a = new BigInteger("1");
multiply(a,0,sc.nextInt());
}
static void multiply(BigInteger b, int loop, int power)
{
BigInteger result = b;
while(loop<power)
{
result = result.multiply(new BigInteger("8"));
loop++;
multiply(result,loop,power);
}
System.out.println(result);
}
}
You call multiplyonly once, but it recursively calls itself (and prints every time). You could return the result instead (and print it from main).

Getting Runtime Error(NZEC) on SPOJ AND CODECHEF?? Couldn't find why this error is occuring on my code

I am getting RUNTIME ERROR(NZEC) on my following code while running my code on competetive programming sites.But could not understand where is the problem in my code as it is running absolutely fine on eclipse:
My code is:
import java.util.*;
public class Main {
public boolean isPalindrome(int number) {
String s1 = String.valueOf(number);
StringBuffer s = new StringBuffer(s1);
String s2 = String.valueOf(s.reverse());
if ((Integer.parseInt(s1)) == (Integer.parseInt(s2)))
return true;
else
return false;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int m1[] = new int[t];
int k=0;
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
for (int j = m + 1;; j++) {
Main main = new Main();
if (main.isPalindrome(j)) {
m1[k++] = j;
break;
}
}
}
for(int l=0;l<m1.length;l++)
{
System.out.println(m1[l]);
}
}
}
Thanks in advance!!
Did you submit this code on CodeChef/Spoj?
Your code seems to be fine but you should provide the input or run your code on an interactive console(as we do in terminal).
You can observe your error by commenting on the scanner lines and initializing the variables in the code itself as I checked this by running it on CodeChef
Else, Try adding this Scanner class.
import java.util.Scanner; // Import the Scanner class
This error is caused when your code returns Non-Zero Error Code(NZEC)
In languages which have exception handling like Java, Python etc we can use exception hadling using try - catch blocks.
NZEC is a runtime error.
for detailed answer to the above question
click here

Working with really big numbers

please have a look at the following code
import java.util.ArrayList;
import java.util.List;
public class Big
{
static int primeNumber = 2;
public static void main(String[]args)
{
int numberDevident = 147;
int left=0;
int result=0;
List numbers = new ArrayList();
while(true)
{
result = numberDevident/primeNumber;
left = numberDevident%primeNumber;
if(left!=0)
{
primeNumber++;
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber);
}
}
}
}
This code find the prime factors of a given number (Variable "numberDevident" in the code"). But, there is a case, that is, the given number is 600851475143
It is no way matching to int, long or double. How can I solve this using this much of a big number? Please help
Here I am doing the same with BigInteger
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class Problem3
{
static BigInteger primeNumber = new BigInteger("2");
static BigInteger zero = new BigInteger("0");
static BigInteger add = new BigInteger("1");
public static void main(String[]args)
{
BigInteger numberDevident = new BigInteger("147");
BigInteger left= new BigInteger("0");;
BigInteger result=new BigInteger("0");;
List numbers = new ArrayList();
while(true)
{
result = numberDevident.divide(primeNumber);
left = numberDevident.remainder(primeNumber);
if(left!=zero)
{
primeNumber.add(add);
}
else
{
numbers.add(primeNumber);
numberDevident = result;
System.out.println(primeNumber.toString());
}
}
}
}
still no good, it is not displaying anything. Please help.
You might want to take a look at BigInteger.

Categories