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

hide comments
2009-08-03 22:07:24 Abdelrahman Hatem
the problems loads in my pc in 3.5 seconds
that's in the worst case.when i send it it tells me time limit exceeded.
Re by Blue Mary: Remember that SPOJ machine is ~0.75 GHz!

Last edit: 2009-08-04 03:41:50
2009-07-17 12:21:18 jiuren
Miller-Rabin Prime is not a good idea
2009-07-05 05:08:30 Yonggang Li
Dammed Miller-Rabin.
2009-07-04 13:30:03 Madan
Hey guys, i've solved this problem in java.I'm using readLine() methods to input from user. When i submitted this source, it says time exceeded. It does not even ask me to input a value. I am new to this place. Could someone help me regarding this?
2009-06-24 08:54:37 wang
DO NOT use ruby Prime Class to solve this problem, sloooooow
2009-06-19 02:27:09 sxpeter
Do remember: 1 is NOT a prime number.
2009-06-18 20:59:06 Rooparam Choudhary
@Sandra

in Main.java declare 'Main' as 'public class Main' and your other class as 'class <Name>'
2009-04-11 18:32:38 seyyed mohammad razavi
yes!!!you must divide numbers to 2 part:(1) befor 1000000 (2)after 1000000

Last edit: 2009-04-11 18:33:00
2009-02-18 13:46:26 Hemant Verma
This problem require double sieve algorithm . First for prime upto 10^5 and then for the prime in range m to n.
2009-02-16 09:00:12 [Trichromatic] XilinX
Please make sure that your algorithm is fast enough to handle input cases like 999900000 1000000000.
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.