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!


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.


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.


1 10
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
Time limit:6s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: NODEJS PERL 6

hide comments
2014-12-21 14:33:44 vivek
Since, the time to print the answer is more than the actiual calculation of the answer, any hints on that side.
2014-12-20 10:18:34 Ketan Sethi
My solution is showing NZEC. I have tested my codE and it runs fine. Any suggestions will be really appreciable


Last edit: 2014-12-20 10:20:07
2014-12-20 07:43:30 Leetao
to my surprise,this problem is so easy.why was correctrate so low?

Last edit: 2014-12-20 08:22:36
2014-12-17 21:23:46 Andreas
Hi! I may be dumb, but I don't get the Input. Should I read it in with an input-method or is it given by a file? And how are the m and n given?

--ans(Francky)--> Before PRIME1, please consider TEST that best suit for a first problem to be solved ; a solution will provided if you get trouble for input/output method.
edit: it seems you already solved TEST. IO are the same on SPOJ ; standard IO.

Last edit: 2014-12-17 22:02:06
2014-12-17 01:41:40 SAURAV SUMAN
my file size limit is execiding can u take me out from this
2014-12-17 00:13:26 Shoumya Kanti Das
The best solutions for this problem shows runtime of 0.0s . How is that possible??
2014-12-16 06:36:30 Ken Ramsey
I used fortran. To any other fortran users, note that you may experience many compilation errors. SPOJ uses gfortran with some GNU extensions that may reject your code for a while until you figure it out. You have to make trivial adjustments, but sadly you're going to have to accept a higher number of compilation errors in your score than you'd like.
2014-12-16 06:23:54 Ken Ramsey
Manan, your program is exceeding the time limit because you are doing a primality test on each number in the given interval. While accurate, that approach is simply too slow. I suggest that you look into the Sieve of Eratosthenes. I used a segmented version of this sieve and it worked fine. A program based on this method is going to spend a lot more time printing out the answer of large intervals than it is in calculating that answer.
2014-12-13 10:30:39 Manan
Can anybody look at the code and identify why it is telling time limit exceeded. http://ideone.com/2VIVdB
2014-12-12 14:03:43 dipankar gupta
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.