/*
Prime Number distance problem
Time Limit: 3000 MS | memory limit: 65535 KB
Difficulty: 2
Description
Now you are given some numbers. You need to write a program to output the adjacent prime numbers of these integers and their length. If left or right
If there is an equal-distance length prime number, the value on the left and the corresponding distance are output.
If the input integer itself is a prime number, the output is the prime number and the distance is 0.
Input
The first line shows the number of test data groups N (0 <n <= 10000)
Each row in the next n Rows has an integer m (0 <m <1000000 ),
Output
Each row outputs two integers a B.
A Indicates the prime number closest to the test data, and B indicates the distance between them.
Sample Input
3
6
8
10
Sample output
5 1
7 1
11 1
*/
// Well, written in C language,
# Include <stdio. h>
# Include <math. h>
Int isprime (int n );
Int main ()
{
Int I, n, num, temp1 = 0, temp2 =-1, flag = 0;
/* While (1)
{
Scanf ("% d", & N );
If (isprime (n ))
{
Printf ("is prime \ n", N );
}
Else
{
Printf ("NO \ n ");
}
}
*/
Scanf ("% d", & N );
While (n --)
{
Scanf ("% d", & num );
If (num = 1)
{
Printf ("2 1 \ n ");
Continue;
}
For (I = 0; I <num; ++ I)
{
Flag ++;
Temp1 = num + I * temp2;
// Printf ("temp = % d \ n", temp1, temp2 );
Temp2 = temp2 * (-1 );
If (temp1> 2 & temp1 % 10% 2 = 0) | (temp1> 3 & temp1 % 3 = 0) | (temp1> 5 & temp1 % 5 = 0 ))
{
If (flag % 2! = 0)
{
-- I;
}
Continue;
}
Else
{
If (isprime (temp1 ))
{
Break;
}
}
If (flag % 2! = 0)
{
-- I;
}
}
If (temp1> num)
{
If (isprime (Num-(temp1-num )))
{
Printf ("% d \ n", 2 * num-temp1, temp1-num );
}
Else
{
Printf ("% d \ n", temp1, temp1-num );
}
}
Else if (temp1 <num)
{
Printf ("% d \ n", temp1, num-temp1 );
}
Else if (temp1 = num)
{
Printf ("% d 0 \ n", num );
}
}
Return 0;
}
Int isprime (int n)
{
Int I = 2,;
A = SQRT (n) + 1;
// Printf ("A = % d % lf \ n", A, SQRT (3.0 ));
If (n = 2)
{
Return 1;
}
For (I = 2; I <= A; ++ I)
{
If (N % I = 0)
{
Return 0;
}
}
Return 1;
}
Http://blog.csdn.net/lp310018931
Nanyang acm24-prime number distance problem