21 / 23
Feb 2016

We can't run this code. Please post it in one block as you would if you were submitting it to the judge. That way we can debug the actual issue.

This takes all of the difficulty out of "solving" the problem. Granted there are many problems that need a few more test cases because they ones they have posted are useless but in general it should be the responsibility of the solver to determine the "useful" test cases.

Here it is:

removed code

Works in ideone.com

Thanks for any ideas

Maybe one question. Is it considered to be an error, if I produce trailing "\n" characters for some inputs?

Thanks

Thanks a lot - I'll fix the code as soon as I can. Hopefully, it was the only problem smile

Finally, solution accepted smile

Feel free to delete this thread or it's parts.

Thanks a lot

Cheers!

In future please remove your own code after getting AC.

Also, if you read the BBOne tutorial in the Problemset Archive section, you'll find that the BBOne tag functions just like a posting to Ideone. It's really quite useful because anyone who can get to the forum can execute your code right on the forum.

4 years later

I´m also getting 4832 primes between 999_900_000 and 1_000_000_000 but still, wrong answer, any other range with primes amoun to compare?
Thanks in advance!

We could post test cases but it would be a waste of our time because they would probably be successful and then our work would have been for nothing. Please post your code.

Hello guys.
Here's my code.
I am getting a time limit exceeded error but it works fine on ideone and even on my system sweat .
Could i get any suggestions or please post any test cases so that i can check it myself?

#include <iostream>
using namespace std;

int main() {
	
	// your code here
	long int in;
	cin>>in;
	while(in>0)
	{
		long int n1,n2;
		cin>>n1>>n2;
		long int x=n1;
		while(x<=n2)
		{
			if(x%2==0 && x!=2 || x==1)
			{
				x++;
			}
			else
			{
				int c=0;
				for(long int i=3;i<x/2;i++)
				{
					if(x%i==0)
					{
						c++;
						break;
					}
				}
				if(c==0)
				{
					cout<<x<<'\n';
				}
				x++;
			}
			
		}
		cout<<'\n';
		in--;
	}
	return 0;
}

Okay i get it.I am a beginner so could you advise on what kind of algorithm to use?

There is no need to use another algorithm for this problem. But your code checks more divisors than it has to. It is not necessary to check divisors up to x/2.

Thanks daft_wullie.
Modified the code as you said.
Now it's WA and for the reference am getting 4832 primes between 999,900,000 and 1,000,000,000.

Here's the code..

include

include

using namespace std;

int main() {

// your code here
long int in;
cin>>in;
while(in>0)
{
	long int n1,n2;
	cin>>n1>>n2;
	long int x=n1;
	while(x<=n2)
	{
		if(x%2==0 && x!=2 || x==1)
		{
			x++;
		}
		else
		{
			int c=0;
			for(long int i=3;i<=sqrt(x);i+=2)
			{
				if(x%i==0)
				{
					c++;
					break;
				}
			}
			if(c==0)
			{
				cout<<x<<'\n';
			}
			x+=2
			;
		}

	}
	cout<<'\n';
	in--;
}
return 0;

}

28 days later

package com.spoj;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Universe {

public void universe()
{
	List<Integer> list = new ArrayList<Integer>();
	Scanner sc = new Scanner(System.in);
	while(true)
	{
		System.out.println();
		int val = sc.nextInt();
		if(val == 42)
		{
			break;
		}
		System.out.println(val);
		list.add(val);
	}
}

public void primeNumberRange() 
{
	//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	Scanner sc = new Scanner(System.in);
	System.out.println();
	int testCase = 0;
	testCase = Integer.parseInt(sc.next());
	System.out.println(testCase);
	String[] rangeArr = new String[testCase];
	int index = 0;

	Scanner sc1 = new Scanner(System.in);
	while(testCase > 0)
	{
		System.out.println();
		String rangeString = null;;
		rangeString =sc1.nextLine();
		rangeArr[index] = rangeString;
		++index;
		--testCase;
	}
	sc.close();
	sc1.close();
	for(int i=0;i<rangeArr.length;i++)
	{
		String[] ranges = rangeArr[i].split("\\s+");
		int lowerRange = Integer.parseInt(ranges[0]);
		int upperRange = Integer.parseInt(ranges[1]);
		List<Integer> primeNumbers = new ArrayList<Integer>();

		primeNumbers.add(2);
		primeNumbers.add(3);

		for(int num=lowerRange ; num <= upperRange;num++)
		{
			int sqrt = (new Double(Math.sqrt(num))).intValue();
			boolean notPrime = false;
			for(Integer prime : primeNumbers)
			{

				if(num ==1 || sqrt == 1)
				{
					notPrime = true;
					break;
				}					
				else if((num != prime && num % prime == 0))
				{
					notPrime =true;
					break;
				}						
			}
			if(!notPrime && (num !=2 || num !=3))
			{
				//System.out.println(num);
				primeNumbers.add(num);
			}
		}
		for(Integer primes :primeNumbers)
		{
			System.out.println(primes);
		}
			System.out.println();
	}

}

public static void main(String[] args)
{
	Universe uv = new Universe();
	uv.primeNumberRange();
}		

}

It shows runtime error when submitted

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.