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
2018-11-01 09:21:07
sqrt(n) approach solution is being accepted. And Sieve of Eratosthenes shows TLE. Can anybody explain this? I can clearly understand that Sieve of Eratosthenes will take up more memory but why is it taking up more time than sqrt(n) approach?
2018-10-31 16:20:57
escobar 'Sieve Of Eratosthenes' is a better approach then 'Segmented Sieve' but still it is giving TLE.
2018-10-31 15:54:58
C++: TLE may be due to the use of the int data type. I use the LL data type and succeed
2018-10-27 20:58:41
TLE is the biggest problem for prime number questions...
2018-10-24 16:07:54
ideone show that my code is working properly, while spoj shows TLE. why would be this happenig?
2018-10-23 14:30:43
i dont know why but my code always get time limit exceded
2018-10-18 15:50:39
If you are traversing from m to n and check every number to be prime or not then complexity will be
O(n-m)*O(sqrt(number)) which will be greater than 10^8 in some cases,eg; when the given number is of around 10^8 or 10^9 magnitude.That's why such solution will get TLE.
Use Segmented Sieve instead.

Last edit: 2018-10-18 15:51:30
2018-10-18 07:15:15
getting a time exceed limit may i know why even though i used sqrt(n) for closing condition of for loop
2018-10-17 16:57:28
same approach. TLE in python3, AC with 1.69s in C++ :|
2018-10-07 11:17:06
i am using sqrt(n) then also its giving me time exceeded may i know why??
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.