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
2015-09-09 15:59:00
Sieve of Erastothenes isn't the fastest way to do this... Just saying.
2015-09-08 21:37:00
I wrote BPSW primality test:) But anyway how some guys have 0.00s on maxtest ? I have 210ms.
2015-09-07 18:52:00
sieve of Eratosthenes works perfectly for this with a little tweak. just operate on the numbers in the range between m and n. Done in 0.41 s
2015-09-07 17:45:46
I used the basic approach to check if a number is prime (go down from his root to number 2 and check if he's divisible by any of numbers. If it's not, then it is a prime) and used the fact that each prime can be written as i*6+1 or i*6-1 (i used that in my while loop, where I increased the counter by 6 and inside checked for mentioned two numbers if they are primes).
I would like to see a link to a good explanation (video or text) of advanced/segmented (is that also called Atkin?) sieve of Eratosthenes, since I think the regular one (original/ ancient Greek) is too slow. Thanks for links!
2015-09-07 15:43:25
Will Sieve of Eratosthenes algorithm work?
2015-09-07 08:18:42
After 11 Wrong Submissions and 4 hours of syapa, Did it finally!!!! :D
2015-09-07 07:48:43
Make sure that the output is exactly same as mentioned...I wasted around 4-5 hrs, just because I didn't have an extra newline between the output of two testcases.
2015-09-06 16:44:10
n-m<=100000
1 <= m <= n <= 1000000000
Check how fast it goes through while checking 100,000 numbers, from 999899999 to 999999999. If it completes within 6s, revert. Else, your algorithm is still too slow.
2015-09-06 10:09:34
My code works fine and is within time limit in ideone then why is the SPOJ judge showing time limit exceeded?

EDIT:My bad,time limit is being exceeded. Can't seem to find a way around it. time complexity is around O(n^2), 2 nested for loops.

Last edit: 2015-09-06 20:26:47
2015-09-04 09:31:39
My code works perfectly in code::blocks. I first enter a number if its less than 1 or more than 10 you need to re enter the number. Next the loop starts with first number as input. He enters two numbers separated by a space each line. Than preses enter. the output is produced as it should be. Still i get wrong answer. What is the reason?

Correction: I was wrong. It was not printing the last number if it is prime.

I have solved it. Answer has been accepted.

Last edit: 2015-09-04 09:40:59
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.