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)
Code:
#include
// #include
// const char *INP="TEST.INP";
int nt[3415];
int N,number;
void Init();
void Test();
int main()
{
Init();
int i;
int t;
long int a,b;
// FILE *f;
// f=fopen(INP,"rb");
// fscanf(f,"%d",&N);
scanf("%d",&N);
for(t=0;t
scanf("%ld%ld",&a,&b);
// fscanf(f,"%ld%ld",&a,&b);
if(a%2==0&&a!=2||a==1) a++;
if(a==2)
{
printf("%d\n",a);
a++;
}
if(a==3)
{
printf("%d\n",a);
a+=2;
}
long int j=a;
while(j<=b)
{
for(i=2;i
if(nt[i]*nt[i]>j) printf("%d\n",j);
j+=2;
}
printf("\n");
}
// fclose(f);
// Test();
// getch();
return 0;
}
void Init()
{
nt[1]=2;
nt[2]=3;
nt[3]=5;
nt[4]=7;
nt[5]=11;
int i=5,j;
int x=13;
while(x<31700)
{
for(j=2;(j<=i)&&(nt[j]*nt[j]<=x);j++)
if(x%nt[j]==0) break;
if(nt[j]*nt[j]>x)
{
i++;
nt[i]=x;
}
x+=2;
}
number=i;
}
void Test()
{
printf("number=%d\n",number);
int i;
for(i=1;i<=number;i++)
{
printf("%d%s",nt[i]," ");
// if(i%100==0)// getch();
}
}
Không có nhận xét nào:
Đăng nhận xét