The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
我们不可能保存2,147,483,647 多个数是不是素数。其实对于一个数n来说,是不是素数,只要判定sqrt(n)内有没有素因子。 所以我们只需要保存sqrt(2,147,483,647) 之内的所有素数就可以判定任意的数是不是素数。
然后对于每个给定的区间[L,R] .我们用小素数来筛出区间内的素数,就可以求出来了。
看代码
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 50000+10; // sqrt(2,147,483,647)=46340
const int MAXM = 1e6+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
bool su[MAXM]={1,1,0}; // 这个大小要是区间的大小1e6
int prm[MAXN],sz=0;
void init(){
for(LL i=2;i<MAXN;i++){
if(!su[i]){
prm[sz++]=i;
for(LL j=i*i;j<MAXN;j+=i){
su[j]=1;
}
}
}
}
LL l,u;
LL prm2[MAXM],cnt;
void make_prm(){
// 筛法
memset(su,0,sizeof(su));
if(l<2) l=2; //防止左边界为1
for(LL i=0;i<sz&&(LL)prm[i]*prm[i]<=u;i++) {
LL s=l/prm[i]+(l%prm[i]>0);// 找到第一个大于等于l的prm[i]的倍数
if(s==1) s=2;//防止l本身为素数,然后下方被判定为非素数
for(LL j=s;j*prm[i]<=u;j++)
if(j*prm[i]>=l)
su[j*prm[i]-l]=1;
}
// TLE的暴力求。
// for(LL i=l;i<=u;i++){
// for(LL j=0;j<sz&&(LL)prm[j]*prm[j]<=i;j++){
// if(i%prm[j]==0||i==1){
// su[i-l]=1;
// break;
// }
// }
// }
cnt=0;
for(LL i=0;i<=u-l;i++){
if(!su[i]) prm2[cnt++]=i+l;
}
}
void solve(){
if(cnt<=1) puts("There are no adjacent primes.");
else{
LL cmin=inf,c1,c2;
LL dmax=-inf,d1,d2;
for(LL i=1;i<cnt;i++){
LL d=prm2[i]-prm2[i-1]+1;
if(d<cmin){
cmin=d;
c1=prm2[i-1];c2=prm2[i];
}
if(d>dmax){
dmax=d;
d1=prm2[i-1];d2=prm2[i];
}
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",c1,c2,d1,d2);
}
}
int main(){
init();
while(scanf("%lld%lld",&l,&u)!=EOF){
make_prm();
solve();
}
return 0;
}