TOJ 1296 Prime Distance

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.

 

题意:求一个区间内相差最近的两个素数和相差最远的两个素数。
         如果区间内只有两个素数,他们既是最远的,也是最近的。
         如果区间内没有两个以上的素数则输出“There are no adjacent primes.”

 

解法:线性筛素法+线性筛素区间版
      "The difference between L and U will not exceed 1,000,000."是可以用线性筛素区间版的先决条件
      1.先筛出0至sqrt(2,147,483,647)的所有素数
      2.1如果所求区间在0至sqrt(2,147,483,647)内,则直接求。
      2.2否则,再以0至sqrt(2,147,483,647)的所有素数筛选大于sqrt(2,147,483,647)
Time: 0.02 sec.     Memory: 5328 K.     Code Length: 1.5 K.
Code:

#include<iostream>
#include<vector>
using namespace std;
const int MAXN=50000;
vector<int>prime;
int num[MAXN];
int su[1005000];
int main()
{
long long n,m,DL,DS,DLx,DLy,DSx,DSy,x,y,i,j,np=0,size,k,ma;
for(i=2;i<MAXN;i++)
{
if(!num[i])prime.push_back(i),np++;
for(j=0;j<np&&i*prime[j]<MAXN;j++)
{
num[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
num[0]=1;
num[1]=1;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
x=-1,DL=0,DS=20000000,DLx=0,DSx=0,y=-1;
if(n<MAXN)
{
for(i=m;i<=n;i++)
{
if(!num[i])
{
if(x==-1){x=i;continue;}
else y=i;
if(y-x>DL)
{
DLx=x;
DLy=y;
DL=y-x;
}
if(y-x<DS)
{
DSx=x;
DSy=y;
DS=y-x;
}
x=y;
}
}
}
else //线性筛素区间版
{
size=n-m;
for(i=0;i<=size;i++)
su[i]=1;
for(i=0;i<np;i++)
{
k=m/prime[i];
if(m%prime[i]!=0)k++;
if(k<=1)k++;
while(k*prime[i]<=n)
{
su[k*prime[i]-m]=0;
k++;
}
}

if(m==1)su[0]=0;
for(i=0;i<=size;i++)
{
if(su[i])
{
if(x==-1){x=i;continue;}
else y=i;
if(y-x>DL)
{
DLx=x+m;
DLy=y+m;
DL=y-x;
}
if(y-x<DS)
{
DSx=x+m;
DSy=y+m;
DS=y-x;
}
x=y;
}
}
}
if(y==-1)printf("There are no adjacent primes./n");
else printf("%lld,%lld are closest, %lld,%lld are most distant./n",DSx,DSy,DLx,DLy);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值