Csu1030 prime number slot

Source: Internet
Author: User
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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.