CSU 1030: Prime slot

Source: Internet
Author: User



Prime slot

Description

       In the adjacent two primes p p + n the n- 1 n 24, +, +,- is in prime number 23 29 A length of 6

Your task is to write a program to calculate The length of the prime slot that contains the integer k. If K itself is a prime number, it is assumed that the length of the prime slot containing K is 0.

Input

The first line is a number N that represents the number of data that needs to be tested. Followed by n rows, each line being a positive integer k,k greater than 1 and is less than or equal to the 100,000th prime number (also known as 1299709).

Output

for each input part of the input k , which corresponds to the output of a non-negative integer that contains k the length of the prime slot, with each non-negative integer occupying one row.

Sample Input
51011272492170
Sample Output
4060114
The code is as follows:
#include <cstdio>bool p[1299710]; #include <cstring>using namespace Std;void prime () {    int i,j;     memset (P,0,sizeof (P));    for (int i=2;i<=1299709;i++)        {           if (!p[i])           {for               (int j=2;i*j<=1299709;j++)                p[i*j]=1;        }}} int main () {    int n,a,sum,k,t;    Prime ();   scanf ("%d", &n);    while (n--)    {        sum=0;        scanf ("%d", &a);        if (!p[a]| | A==1)          {             printf ("%d\n", sum);              Continue;          }        T=a;        while (a--)        {            if (P[a])            sum+=1;            else break                ;        }        while (t++)        {            if (p[t])            sum+=1;            else break                ;        } printf ("%d\n", sum+2);    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CSU 1030: Prime slot

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.