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.

hide comments
faronnok093: 2021-05-03 13:51:07

simple segmented Sieve problem easy to solve

camilyed: 2021-05-03 10:42:00

And also you can use ECPP, but is very complicated.

dgenxsid: 2021-04-30 11:52:21

We could also iterate through L to R and check if it's prime or not and then print the number if it's prime.
For checking, we can use the Miller Rabin test.

terminator2001: 2021-04-30 08:23:08

SEGMENTED SIEVE is the way to go! You can not use normal sieve either because it can work up only to 10^8 and this question is for numbers greater than that.

anujpatidar3: 2021-04-24 21:23:39

@yshi2021 modify segmented sieve that will work

yshi2021: 2021-04-19 16:33:17

The code worked fine on the input from the example. But got error of "time limit exceeded". Why?

siddik_718: 2021-04-19 13:56:42

with segmented sieve and cin 0.01

charlieliujcn: 2021-04-18 15:05:41

I used Sieve in Python3, but always got timelimit error. anybody worked out solution with python. will try java to see if get it faster,

anujpatidar3: 2021-04-17 13:08:17

Segmented Sieve will do it I think

aditya22_jadav: 2021-04-14 11:05:35

in c++14


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