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
Shoumya Kanti Das: 2014-12-17 00:13:26

The best solutions for this problem shows runtime of 0.0s . How is that possible??

Ken Ramsey: 2014-12-16 06:36:30

I used fortran. To any other fortran users, note that you may experience many compilation errors. SPOJ uses gfortran with some GNU extensions that may reject your code for a while until you figure it out. You have to make trivial adjustments, but sadly you're going to have to accept a higher number of compilation errors in your score than you'd like.

Ken Ramsey: 2014-12-16 06:23:54

Manan, your program is exceeding the time limit because you are doing a primality test on each number in the given interval. While accurate, that approach is simply too slow. I suggest that you look into the Sieve of Eratosthenes. I used a segmented version of this sieve and it worked fine. A program based on this method is going to spend a lot more time printing out the answer of large intervals than it is in calculating that answer.

Manan: 2014-12-13 10:30:39

Can anybody look at the code and identify why it is telling time limit exceeded. <snip>

Last edit: 2022-06-19 12:39:02
dipankar gupta: 2014-12-12 14:03:43

done......!!!!!

Anonymous: 2014-12-10 08:40:06

FINALLY....found out the bug..thanks SPOJ :)

Anonymous: 2014-12-10 08:30:59

My solution is showing SIGSEGV. I have tested my code for most extreme cases on my system and it runs fine...but problem persists with SPOJ. Any suggestions?

Anonymous: 2014-12-08 16:10:03

What is the problem with my code? I know its brute force...but my answer is correct. And SPOJ is just not accepting it...why??

Thanks :)

(Reply by Min_25)
Please see the Notes below.
> 1. Don't post any source code here.
> 3. For more discussion (hints, ideas, solutions) please visit our forum.

Last edit: 2014-12-08 16:55:14
sahil bhatia: 2014-12-08 14:22:17

how to minimize the runtime error please tell .. i am finding hell lot of problem with these runtime errors

Ghareeb: 2014-12-07 23:38:48

the code is ok and works very well but it is still wrong x@@


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