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)


After cluster change, please consider PRINT as a more challenging problem.
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 SCM chicken

hide comments
2015-03-06 08:47:10 Abhinandan Agarwal
Finally in 0.01 time :-)
2015-03-04 15:10:22 Shubham Mohanka
@ron tle (time limit exceeded) is an error in case when ur program is not fast enough to complete in the given time limit.try using a faster algorithms.if u r a beginner,i will suggest u to google faster primality testing algos.hope that answers ur query.
this might help - http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=primalityTesting
cheers :)

Last edit: 2015-03-04 15:11:21
2015-03-04 07:49:08 ron
my program does what the problem is asking for but when I submit it shows TLE. can someone tell me why TLE?
2015-03-02 16:51:58 fordex
whats the best algorithm to generate primes between any two numbers?

Last edit: 2015-03-02 16:52:41
2015-02-28 15:03:48 Nootan R
2015-02-21 06:43:42 Shashank kumar
no need of seives ...just read an article on wikipedia...about primality test http://en.wikipedia.org/wiki/Primality_test
2015-02-19 09:16:56 Biswajit Sahu
I'm using Python 2.7.
I'm getting NZEC error. Serached for it.
I also used sys.exit(0).
Still geeting the error.
Pls help

re(vamsi): sys.exit(0) leads to runtime error in spoj. avoiding doing that(you can always write an alternative to sys.exit) and please don't spam the comment section(if query is not related to problem) use forum or python subfourm.

Last edit: 2015-02-19 12:31:17
2015-02-18 10:54:46 SANKET MAKHIJA
sometimes basic worth it,,, no use of seive..
2015-02-16 07:03:58 Jagan HS

Last edit: 2015-02-16 07:04:55
2015-02-15 15:28:20 Siddhant Mishra
i am getting a runtime error
can someone help

(Francky)=> Please use forum for help, as pointed by the notes below.

Last edit: 2015-02-15 16:26:25
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.