Describe
Now give you some number, ask you to write a program, output these integers adjacent to the nearest prime, and output its distance between the length. If there is an equidistant length prime, the value of the left side and the corresponding distance are output.
If the input integer itself is a prime number, the output of the prime number itself, the distance output 0
-
-
Input
-
-
the first line gives the number of test data groups N (0<n<=10000)
The next n rows have an integer M (0<m<1000000) per line,
Output
outputs two integers a B per line.
Where a represents the nearest prime number to the corresponding test data, and B indicates the distance between them.
Sample input
36810
Sample output
5 17 111 1
#include <iostream> #include <cmath>using namespace std;bool isprime (int lhs) {for (int i = 2; I <= sqrt (LHS) ; i++) {if (!) ( LHS% i)) return false;} return true;} int main () {int n, m,m1,m11,m2,m22,t1,t2,i;bool flag;cin >> n;while (n--) {cin >> m;m1 = m2 = M;m11 = M22 = 0;t1 = t2 = 0;if (1==m) {cout << 2 << "" << 1 << Endl;} Else{while (M1) {if (IsPrime (M1)) {M11 = M1;t1 = M-m11;break;} --M1;} while (m2) {if (IsPrime (m2)) {M22 = M2;t2 = M22-m;break;} ++m2;} if (T1 > T2) cout << m22 << "<< T2 << endl;elsecout << M11 <<" "<< T1 < ;< Endl;}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nyoj 24 Prime distance problem