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-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
2014-09-10 03:34:57 sara
TLE exceeded!!
please help me i don't konw what to do :(
2014-09-08 20:08:24 comptechie
TLE exceeded! :(
plz help me out!
urgent!

http://ideone.com/m1ExJk
2014-09-08 03:42:12 adrian hartanto osman
runtime error NZEC why?
2014-09-07 20:02:49 vutsal
http://www.codeskulptor.org/#user37_wWgOhNyiFQ_0.py

guys please help me its showing time limit exceded.....!! :(
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.