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
dharmbhusan_0: 2016-02-06 07:09:51

where am i rong when i am writting prime generator program on gcc then it is ok but when i am writting on spoj then time limit exceeded shown plz help me

shirish_heller: 2016-02-05 20:36:37

plzz help code output is perfectly fine ... gives success on ideone also still ... time limit exceeded when i submit???

//CODE//
#include <stdio.h>

int main(void) {
long int n1,n2;
int N,i,j,count=0;
scanf("%d",&N);
while(N)
{
N--;
scanf("%lu%lu",&n1,&n2);
for(i=n1;i<=n2;i++)
{
for(j=2;j<(i-1);j++)
{
if(i%j==0)
{
count++;
break;
}
}
if((count==0)&&(i!=1))
{
printf("\t %d \n",i);
}
count=0;

}
printf("\n ");

}


return 0;
}

shekhar7: 2016-02-04 20:49:09

@ basmaashouur: even if it passes TLE it would give you WRONG ANSWER as u will consider 1 as also prime... and to remove TLE change "for(long m=2;m<=y/2;m++)"
to " for(long m=2;m<=sqrt(y);m++)"...... and dont use long simply use int as int can store upto 10^9 :)

basmaashouur: 2016-02-04 08:12:05

#include <iostream>
#include <string>
#include <stdio.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
int main() {
long size;
int flag =0;
cin >> size;
long long a[2];
//loop btmshy 3la el awl rkmeel then tany rkmeen
for (int x = 0; x < size ; x++)
{
cin >> a[0] >> a[1] ;
// loop btmsk kol el arkam ma been el rkmeen
for(long y =a[0]; y<=a[1]; y++)
{// loop btshouf awl rakm da prime wla la2
for(long m=2;m<=y/2;m++){
if (y%m == 0)
{
flag = 1;
break;
}
}
if (flag==0)cout<<y<<endl;
flag = 0;
}
cout << " " << endl;
}

return 0;

}
hey guys, can you please tell me why my code brings time limit exceeded?

dkumarsingh: 2016-02-01 19:16:50

a silly mistake got me 2 WA... a nice problem....

abd_alrhman: 2016-01-31 12:27:00

how i can show the output of my wrong submission

psurya: 2016-01-31 10:30:56

can anyone help me, i dont know whats wrong about my code but its not showing any output:

//prime generator

#include <iostream>
using namespace std;

int main(void)
{
int num,count=0; //for test cases and prime counter
cout<<"how many no. of test cases you want to give?";
cin>>num;

for( int i = 0; i<num; i++) //the loop will run num number of times
{
int a,b;
cin>>a>>b;

for(int j=a; j<=b; j++) //for generating prime numbers from a to b
{

for(int k=1; k<=j; k++) //for checking condition of prime no. for a to b
{
if( j%k == 0)
count++;

}
if( count == 2) //means if j is divisible by 1 and itself
cout<<j<<endl;

}

cout<<"\n";
}


return 0;
}

ulti72: 2016-01-31 08:33:38

What does for every test case means

aktech: 2016-01-29 19:44:31

This may help: http://iamit.in/blog/Spoj-Problem-Small-Factorials-FCTRL2-Explanation-and-Solution/

enessetere: 2016-01-29 09:23:06

is there any chance to see where and what "wrong answer" appears?


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 PERL 6