Prime distance

Basic meaning

Given two integers L and R, you need to find the closest two adjacent prime numbers C1 and C2 in the closed interval [L,R] (ie C2-C1 is the smallest), if there are other phases with the same distance For adjacent prime number pairs, the first pair is output.
At the same time, you also need to find the two adjacent prime numbers D1 and D2 with the farthest distance (that is, D1-D2 is the largest). If there are other adjacent prime number pairs with the same distance, output the first pair. Input format
Enter two integers L and R per line, where the difference between L and R will not exceed 1000000.
Output format
For each L and R, output a result, and the result occupies one line.
The result includes the nearest neighbor prime number pair and the farthest neighbor prime number pair. (Refer to the sample for the specific format)
If there is no prime number pair between L and R, output "There are no adjacent primes.".
Data range
1≤L<R≤2^31−1
Input example:
2 17
14 17
Output example:
2,3 are distant closest, 7,11 are most.
There are no adjacent primes.

The basic idea

1. This question must sift prime numbers, consider using Euler sieve with higher efficiency.
2. The range of L and R is very large, it is impossible to screen out all the prime numbers in [2,R]. But we noticed that the value of RL is very small, so consider an algorithm that only generates prime numbers in [L,R]

Mathematical Algorithm and Proof

1. Sieve out all the prime numbers in [2,sqrt( R )]
2. According to each composite number must contain a prime factor not greater than sqrt(N), for each prime number p, put i*p([L/ p]<=i<=[R/p]) is marked as a composite number
3. The rest is the prime number sought

Code

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1000005;
int prime[N],cnt;
bool st[N];
typedef long long ll;
void get(int n){
    
    //欧拉筛代码
    memset(st,false,sizeof st);
    cnt=0;
    for(int i=2;i<=n;i++){
    
    
        if(!st[i]) prime[cnt++]=i;
        for(int j=0;prime[j]*i<=n;j++){
    
    
            st[prime[j]*i]=true;
            if(i%prime[j]==0) break;
        }
    }
}
int main(){
    
    
    ll l,r;
    while(cin>>l>>r){
    
    
        get(50000);
        memset(st,false,sizeof st);
        for(int i=0;i<cnt;i++){
    
    
            int p=prime[i];
            for(ll j=max((l+p-1)/p*p,2ll*p);j<=r;j+=p){
    
    //求比l大的最小的p的倍数,即ceil(l/p)*p。而通常将ceil(a/b)写成floor((a+b-1)/b)。
                st[j-l]=true;//坐标偏移,类似离散化
            }
        }
        cnt=0;
        for(int i=0;i<=r-l;i++){
    
    
            if(!st[i] && i+l>1) prime[cnt++]=i+l;//要特判1的情况
        }
        if(cnt<2) puts("There are no adjacent primes.");
        else{
    
    
            int maxp=0,minp=0;
            for(int i=0;i+1<cnt;i++){
    
    
                int d=prime[i+1]-prime[i];
                if(d<prime[minp+1]-prime[minp]) minp=i;
                if(d>prime[maxp+1]-prime[maxp]) maxp=i;
            }
            printf("%d,%d are closest, %d,%d are most distant.\n",prime[minp],prime[minp+1],prime[maxp],prime[maxp+1]);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_55341679/article/details/114155066