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