PRIME1 - Prime Generator

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)

Information

After cluster change, please consider PRINT as a more challenging problem.

Added by:Adam Dzedzej
Date:2004-05-01
Time limit:6s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6

hide comments
2016-01-17 09:09:11
I'm implementing sieve in Python 2.7. However, I get TLE, which I believe is due to the fact that I create a second list from 2 to the first number and then delete find multiples of the numbers in the new list and delete the occurrences of those multiples in the actual list. Is there a faster way for me to do this?

Last edit: 2016-01-17 09:40:21
2016-01-17 07:24:45 Kumud Khandelwal
there is no need to use sieve
this is a very easy question, also very irritating.
use simple method.
2016-01-15 18:52:59
@sakib_muhit
its not necessary that all 6*n-1 or 6*n+1 numbers will be prime
but vice-versa is true.
better, use sieve smartly :)
2016-01-13 22:14:32 Marcin Blacharski
#sid_d01
You have to write (i%2 == 0), because % returns a number, so how will you negate a number? :)
2016-01-07 23:50:54
@yip37: - try segmented sieve
2016-01-07 05:35:49
What I don't get is that if I use sieve I must build an array or vector of size 1.000.000.000-1 , since the algorithm must start from 2. If n is maximum then it has to be that size, and that always yields a memory error. When I tried without the sieve it was too slow, but I'll try again see if I can optimize it.
2016-01-06 17:14:16
This is really very irritating problem.... however it taught me a lot.
2016-01-05 04:12:04
don't it using sieve for the first 10^5 primes and then trail for the rest :) !! it's a good problem and could be solved in a lot of interesting ways !! :)
2016-01-03 15:25:11
Ideone does not understand (!i%2). you have to write (i%2==0)?
2016-01-02 15:09:36
even by using sieve-of-eratosthenes algo i am getting tle can anyone suggest somother algo for this prob.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.