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
shshnk28: 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

casper_8: 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!

kiner_shah: 2015-09-07 15:43:25

Will Sieve of Eratosthenes algorithm work?

durty_nuke: 2015-09-07 08:18:42

After 11 Wrong Submissions and 4 hours of syapa, Did it finally!!!! :D

mkatiyar: 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.

lagboy: 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.

estelishope: 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
amitray: 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
eapostol: 2015-08-31 05:55:09

can't get it to work on ideaone either, getting timeout error here but works fine locally.

adityavasistha: 2015-08-30 17:35:29

showing time out


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