Sexy primes are pairs of primes of the form (p, p+6), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)
Now given an integer, you are supposed to tell if it is a sexy prime.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (<=10^8).
Output Specification:
For each case, print in a line
Yes
if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, printNo
instead, then print in the next line the smallest sexy prime which is larger than N.
Sample Input 1:
47
Sample Output 1:
Yes
41
Sample Input 2:
21
Sample Output 2:
No
23
【AC代码】简单素数判断—不能再简单了~
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int x) 5 { 6 if(x < 2) return false; 7 for(int i = 2; i*i <= x; i++) 8 if(x % i == 0) return false; 9 return true; 10 } 11 12 int main() 13 { 14 int n; cin >> n; 15 if(isPrime(n)) 16 { 17 if(isPrime(n-6)) 18 { 19 cout << "Yes" << endl << n-6; 20 return 0; 21 } 22 else if(isPrime(n+6)) 23 { 24 cout << "Yes" << endl << n+6; 25 return 0; 26 } 27 } 28 else{ 29 while(1) 30 { 31 n+=1; 32 if(isPrime(n) && isPrime(n+6)) 33 { 34 cout << "No" << endl << n; return 0; 35 } 36 if(isPrime(n) && isPrime(n-6)) 37 { 38 cout << "No" << endl << n; return 0; 39 } 40 } 41 } 42 }