POJ2689 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.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
bool is_prime[maxn];
bool is_prime_small[maxn];
ll prime[maxn];
ll prime_num=0;

//对区间[a,b)内的整数执行筛法,is_prime[i-a]=true  ---  表示i是素数 注意这里下标偏移了a,所以从0开始。
void segment_sieve(ll a,ll b) {
    for(ll i=0;i*i<=b;++i) is_prime_small[i]=true; //对[2,sqrt(b))的初始化全为质数
    for(ll i=0;i<=b-a;++i) is_prime[i]=true; //对下标偏移后的[a,b)进行初始化

    for(ll i=2;i*i<=b;++i) {
        if(is_prime_small[i]) {
            for(ll j=2*i;j*j<=b;j+=i) is_prime_small[j]=false;  //筛选[2,sqrt(b));
            //(a+i-1)/i得到最接近a的i的倍数,最低是i的2倍,然后筛选
            for(ll j=max(2LL,(a+i-1)/i)*i;j<=b;j+=i) is_prime[j-a]=false;
        }
    }
    for(ll i=0;i<=b-a;++i)  //统计个数
        if(is_prime[i]) prime[prime_num++]=i+a;
}

int main()
{
    ll a,b,ans1,ans2,ans3,ans4,dis1,dis2;
    while(~scanf("%lld%lld",&a,&b))
    {
        if(a==1)
            a++;
        prime_num=0;
        dis1 = 0;
        dis2 =  1000010;
        memset(prime,0,sizeof(prime));
        segment_sieve(a,b);
        for(ll i=1;i<prime_num;++i)
        {
            //printf("%d",prime[i]);
            if(prime[i]-prime[i-1]>dis1)
            {
                dis1 = prime[i]-prime[i-1];
                ans1 = prime[i-1];
                ans2 = prime[i];
            }
            if(prime[i]-prime[i-1]<dis2)
            {
                dis2 = prime[i]-prime[i-1];
                ans3 = prime[i-1];
                ans4 = prime[i];
            }
        }
        //cout<<prime_num<<endl;
        if(dis1==0)
            printf("There are no adjacent primes.\n");
        else
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n",ans3,ans4,ans1,ans2);
        //printf("%lld\n",prime_num);
    }
    return 0;
}

优质内容筛选与推荐>>
1、ES6中的Promise.resolve()的作用
2、Create-React-App项目外使用它的eslint配置
3、logback 范例
4、input button 的 onserverclick 事件
5、物体的惯性旋转


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn