Description
Two Adjacent prime numbersPAndP + nBetweenN-A sequence composed of one continuous sum is called the lengthN. For example, limit 24, 25, 26, 27, 28 › is a prime number slot with a length of 6 between prime number 23 and prime number 29.
Your task is to write a program to calculate the inclusion integerKThe length of the prime number slot. IfKIt is a prime number, so it is considered to containKThe length of the prime number slot is 0.
Input
The first line is a number.NIndicates the number of data to be tested. There areNRow. Each row is a positive integer.K,KThe Second Prime Number (that is, 100,000) that is greater than 1 and less than or equal ).
Output
For each input partK, All correspond to the output of a non-negative integer, indicating to containKThe length of the prime number slot. Each non-negative integer occupies a row.
This topic requires no time-out. Therefore, you need to use the prime number tabulation method to locate the qualified prime number. The final result is obtained by subtracting the subscript.
The Code is as follows:
1 #include <iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 7 #define MAXN 1300000 8 int p[MAXN]; 9 10 void getPrim()11 {12 memset(p,1,sizeof(p));13 int n = (int)(sqrt(MAXN+0.5));14 for(int i=2;i<=n;i++){15 for(int j=i+i;j<=MAXN;j+=i) p[j]=0;16 }17 }18 int main()19 {20 int T,k;21 cin>>T;22 getPrim();23 while(T--){24 cin>>k;25 26 int top=k,floor=k;27 while(p[top]==0) top++;28 while(p[floor]==0) floor--;29 cout<<top-floor<<endl;30 31 }32 return 0;33 }