Sphere Online Judge

SPOJ Problem Set (classical)

2. Prime Generator

Problem code: PRIME1

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)
Added by:Adam Dzedzej
Date:2004-05-01
Time limit:6s
Source limit:50000B
Languages:All except: CLOJ F# PERL 6 TECS

hide comments
2010-07-18 12:15:57 nikhil
time limit exceed mean??????
2010-07-14 07:28:49 Diogo F. S. Ramos
Two main advices from whom who just beat it:

* Runtime errors can be caused by a excessive use of memory. If you are using "sieve of Eratosthenes", just think how much memory would be used.

* If you are using "sieve of Eratosthenes" -- which were my case -- think how the algorithm mark the non-primes and ask yourself: Do I *really* need to find _all_ the primes up to /n/? Is this what the problem asks?

Last edit: 2010-07-14 07:30:41
2010-07-13 13:04:26 cegprakash
my program computes even the worst case in 4 seconds. Given time limit is 6 seconds. But why i'm getting time limit exceeded?



Last edit: 2010-08-12 20:27:17
2010-07-10 22:58:00 Paradise
getting TLE even after using sieve algorithm ...
2010-07-08 11:29:33 Florian Beuter
can anyone confirm that AKS primality test works in the given time limit?
2010-07-04 16:35:33 Harits E.
got time limit exceeded... -__-

does the time when the program outputting all the numbers is counted into the time limit too?

EDIT: Nevermind, i passed, 0.10 sec.

Last edit: 2010-07-04 18:50:23
2010-06-29 05:21:55 kaushik.mv
Same solution is randomly giving 0.05,0.06,0.07 sec at different submissions...lol......!!!

Last edit: 2010-06-29 05:29:46
2010-06-16 23:10:07 here for optimisation
got it upto 0.05 sec :)
but...still wondering how 2 get it 2 0.02 :(

Last edit: 2010-06-17 07:38:33
2010-06-16 21:41:41 Rudolf Brisuda
I tested my code on maximall case and it take about 3 sec (CPU 1,6 Ghz ), but here I get still TLE, what is wrong?

Last edit: 2010-06-16 21:52:15
2010-06-15 12:33:18 here for optimisation
my code here runs in 3.56 secs...i used sieve
can't think how to get it under 0.1 sec...
any suggestions?
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.