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-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
2015-02-14 15:30:51 Sarvesh Kesharwani
use sieve to solve this question naive algo can also solve because of cube
2015-02-14 13:34:09 Sanjay Gupta
My problem running time is .1 as per IDEone but still Spoj showing time limit exceed so Please suggest me How can i submit solution.

re(vamsi): try to run your code against higher values of m, n. use forum.(post your code there incase you have any further queries)

Last edit: 2015-02-19 12:34:45
2015-02-12 22:12:40 Archit Kapoor
Sooo after 17 attempts, I finally made it.. AC in the 18th attempt...!! Phew..
2015-02-11 19:18:01 Madhav
very good question!!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.