“性感素数 ”是指形如 $(p,p+6)$ 这样的一对素数。
之所以叫这个名字,是因为拉丁语管“六”叫“$sex$”(即英语的“性感”)。
现给定一个整数,请你判断其是否为一个性感素数。
输入格式
输入在一行中给出一个正整数 $N$。
输出格式
若 $N$ 是一个性感素数,则在一行中输出 $Yes$,并在第二行输出与 $N$ 配对的另一个性感素数(若这样的数不唯一,输出较小的那个)。
若 $N$ 不是性感素数,则在一行中输出 $No$,然后在第二行输出大于 $N$ 的最小性感素数。
数据范围
$1≤N≤108$
输入样例1:
47
输出样例1:
Yes
41
输入样例2:
21
输出样例2:
No
23
C++代码
#include <bits/stdc++.h>
using namespace std;
bool yes(int n){
if (n == 1) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
for (int i = 2; i * i <= (n + 6); i++)
if ((n + 6) % i == 0)
return false;
return true;
//判断素数
}
int main() {
int n;
cin >> n;
if (n == 5){
cout << "Yes" << endl << 11 << endl;
return 0;
//因为 5 这个数字很特殊,所以要打表
}
else if ((yes(n) == true) or (yes(n - 6) == true and n > 6)){//判断是否成立
if (yes(n - 6) == true) cout << "Yes" << endl << n - 6 << endl;
else cout << "Yes" << endl << n + 6 << endl;
return 0;
} else {
cout << "No" << endl;
for (int i = n + 1; ; i++)
if ((yes(i) == true) or (yes(i - 6) == true and i > 6)){//判断是否成立
cout << i << endl;
return 0;
}
}
}
//完结撒花
clean(无注释)
#include <bits/stdc++.h>
using namespace std;
bool yes(int n){
if (n == 1) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
return false;
for (int i = 2; i * i <= (n + 6); i++)
if ((n + 6) % i == 0)
return false;
return true;
}
int main() {
int n;
cin >> n;
if (n == 5){
cout << "Yes" << endl << 11 << endl;
return 0;
}
else if ((yes(n) == true) or (yes(n - 6) == true and n > 6)){
if (yes(n - 6) == true) cout << "Yes" << endl << n - 6 << endl;
else cout << "Yes" << endl << n + 6 << endl;
return 0;
} else {
cout << "No" << endl;
for (int i = n + 1; ; i++)
if ((yes(i) == true) or (yes(i - 6) == true and i > 6)){
cout << i << endl;
return 0;
}
}
}