Sphere Online Judge

SPOJ Problem Set (classical)

2. Prime Generator

Problem code: PRIME1


Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input

The input begins with the 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 (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5
Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)
Added by:Adam Dzedzej
Date:2004-05-01
Time limit:6s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6

hide comments
2014-09-23 21:07:01 Naresh Durgam

#include<iostream>
using namespace std;
int main()
{
int i,j,a,b,t;
cin>>t;
if(t<=10)
for(int k=0;k<=t;k++)
{
cin>>a;
cin>>b;
if((a<b<1000000000)&&(b-a)<100000)
{
for(int i=a;i<=b;i++)
{
for(j=2;j<=i;j++)
{
if(!(i%j)&&(i!=j)) break;
if(j==i)
{
cout<<i<<endl;
}
}
}
}
}
return 0;
}
what is wrong in this code?
it is always showing compaliation error?
please some help me where i'm doing wrong
2014-09-22 20:07:08 Nooger
what's SIGSEGV ?



Last edit: 2014-09-22 20:52:41
2014-09-22 17:13:56 vikram sankhala
very easy one .................ac in one go......my 12th on spoj
2014-09-22 04:54:18 facug91
Sadi: maybe because your class name is PRIME1 and not Main, as it should for spoj.com in Java. Or might be because you haven't close the Scanner (sc.close())...

Last edit: 2014-09-22 04:56:03
2014-09-21 07:23:15 Sadi
import java.util.Scanner;
import java.util.ArrayList;

public class PRIME1 {
public boolean prime(int p){
if (p<2)
return false;
for (int i=2; i<=Math.sqrt(p); i++){
if(p%i==0)
return false;
}
return true;
}
public ArrayList primes(int a, int b){
ArrayList result= new ArrayList();
for (int i=a; i<=b; i++){
if(prime(i))
result.add(i);
}
return result;
}
public static void main(String[] args){
PRIME1 prm=new PRIME1();
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
ArrayList[] results= new ArrayList[t];
for (int i=0; i<t; i++){
int a=sc.nextInt(), b=sc.nextInt();
results[i]=prm.primes(a, b);

}
for (int i=0; i<t; i++){
for(int j=0; j<results[i].size(); j++){
System.out.println(results[i].get(j));

}
System.out.println();
}
}

}
why compilation error?
2014-09-21 01:06:16 Craig
For those with "it works on ideone.come, but TLEs here", please note : "Intel Pentium III 733 MHz". You need to code to that limitation. Likewise for "256MB"

Last edit: 2014-09-21 01:07:15
2014-09-20 17:42:42 diff_edge
NZEC means Non-Zero Exit Code. Which means, you forgot a "return 0" at the end.
2014-09-15 17:40:43 Giridhar Metikal


Last edit: 2014-09-15 17:42:31
2014-09-14 17:42:13 Nihar Tadichetty
runtime error nzec why?

Last edit: 2014-09-14 18:36:58
2014-09-10 19:52:43 Dobrila
why does this exceed time limit...?
http://ideone.com/e8MvRn
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.