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-07-29 14:58:22 Michael Moerschell
@Shaurya Dwivedi
Brute force won't work at all. Check out this: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
2014-07-29 08:23:18 Shaurya Dwivedi
time limit exceeded. :(
please help.
http://ideone.com/e.js/tG7P0I
2014-07-28 19:31:22 Samar Holkar
ac with segmented seive...
2014-07-28 08:53:34 Daga
got ac...at last!!
2014-07-26 22:14:38 cerberuss
finally accepted after few mistakes.. :)
2014-07-26 19:25:37 Deepanshu Thakur


Last edit: 2014-07-26 21:11:37
2014-07-26 19:21:04 Deepanshu Thakur
Wrong why my CODE is absolutely right
http://ideone.com/eTYVF1
http://ideone.com/QUFDji
2014-07-26 09:23:04 chetan sawant
wrong why? http://ideone.com/EPKucO
2014-07-26 06:32:42 Amartya Sanyal
http://ideone.com/ERmIvQ
This runs fine on IDEONE but gives runtime error NZEC :/
2014-07-26 04:48:34 Rahul Gosain
please, tell me how has my code exceeded the time limit.
It's probably the easiest way to solve it.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.