poj 2689 Prime Distance 二次筛法

来源:互联网 发布:suse linux 安装教程 编辑:程序博客网 时间:2024/05/16 15:58
Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10269 Accepted: 2772

Description

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 1714 17

Sample Output

2,3 are closest, 7,11 are most distant.There are no adjacent primes.

Source

Waterloo local 1998.10.17
求给定区间内的相邻素数之差的最大值与最小值
用筛选法求给定区间内的素数,再求相邻素数之差的最大值与最小值
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#define N 1000010#define inf 2147483647using namespace std;#define ll long longbool isP[N];ll p[N];int pn;ll s,t;void init(){    isP[1] = true;    pn = 0;    for(int i=2; i<N; i++)    {        if(!isP[i])        {            p[++pn] = i;            for(int j=i; 1ll*j*i<N; j++)                isP[i*j] = true;        }    }}int main(){    init();    ll a,b,i,j;    ll s,t;    ll maxd,mind;    ll x1,y1,x2,y2;    while(scanf("%lld%lld",&a,&b)!=EOF)    {        if(a<2) a = 2;        s = t = -1;        maxd = -1;        mind = inf;        memset(isP,false,sizeof(isP));        for(i=1; i<=pn&&p[i]*p[i]<=b; i++)        {            j=max(a/p[i],p[i]);            for(; p[i]*j<=b; j++)            {                if(p[i]*j<a) continue;                isP[p[i]*j-a] = true;            }        }        for(i=a; i<=b; i++)        {            if(!isP[i-a])            {                s = t;                t = i;                if(s!=-1&&t!=-1)                {                    if(mind>t-s)                    {                        mind = t-s;                        x1 = s;                        y1 = t;                    }                    if(maxd<t-s)                    {                        maxd = t-s;                        x2 = s;                        y2 = t;                    }                }            }        }        if(maxd==-1) printf("There are no adjacent primes.\n");        else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",x1,y1,x2,y2);    }}


原创粉丝点击