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!


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.


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.


1 10
3 5


Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)


After cluster change, please consider PRINT as a more challenging problem.

hide comments
hemezh: 2013-03-13 22:12:29

@Botta: have a look on

Giovanni Botta: 2013-03-13 22:12:29

Really struggling with this one.
@Rakib: I think that's the way to go for Erathostenes sieve, but I can't figure out how to properly index the integer into a bitset, that is, how given a number you can find its corresponding bit in the array of chars (or 32 bit integer) without being forced to perform modulo operations. I'm sure there is a way but my discrete math skill is not good enough to figure it out.
By the way, if one chooses a 32 bit word, the prime numbers will have the form: 120n+1, 120n+7, ..., 120n+31, ... etc.

David Winiecki: 2013-03-13 22:12:29

Diogo's second comment about the Sieve was really helpful. I can't believe how effective that one change was.

Mukul: 2013-03-13 22:12:29

I think this problem should be solve using Sieve algorithm, otherwise you will get TLE as i got.

Adrian Kuegel: 2013-03-13 22:12:29

Try this case:
1 1

Manuel Meder: 2013-03-13 22:12:29

hi, I'm confused now. I submitted my code 5333234 with "initial error" then I submitted that code again and get "wrong answer" tho if I calculate the Primes from 1 to I get the 50847534 Primes returned. (So if the Primes should be okay, how come it says wrong answer) each set is separated by a blank line and the after the last set theres just a \n.
For equal numbers (from x to x) there is also a blank line, is that the mistake?

Edit: Problem solved! Adrian's hint helped.

Last edit: 2011-07-04 11:36:00
VICTOR JOHANN CORTEZ: 2013-03-13 22:12:29

1 100001
100002 200000
999990000 1000000000
999900000 1000000000
999900000 1000000000
1 100001
999990000 1000000000
999900000 1000000000
999990000 1000000000
1 100001

try this input to calculate...

Dan: 2013-03-13 22:12:29

By definition, a prime number is
1. Must be bigger than 1
2. The number must be divisible only by one and itself

Actually, the m|n limit was the key for solving the problem (on my end)

Happy thinking everyone :)

:D: 2013-03-13 22:12:29

Please move your code to the forum (probem set archive section). Comments are no place for the whole programs, your spoiling it for others!

Alca: 2013-03-13 22:12:29

I use Miller-Robbin( O(log n) )
and I got AC by C++ use 2 secs.
but when I write the Algorithm by Python, I got TLE.. I saw some people use Python and got AC with very short time. How can they do it?

Added by:Adam Dzedzej
Time limit:6s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6