输入样例:
20
输出样例:
4
代码:
#include <stdio.h>
#include <math.h>
int main()
{
/**i初值为3,代表从3开始判断,小于三,则couple = 0
***n初值为2,表示第一个素数为2
**/
int num = 0, couple = 0,i = 3, n = 2, j = 3, loop = 0,m;
char ch;
scanf("%d",&num);
//边界判断
if(num <= 0 || num >= 100000){
return 0;
}
//对符合条件的值进行判断
while(i <= num){
//偶数直接跳出
if(i % 2 == 0){
i++;
continue;
}
//判断当前值是否为素数,不是则跳出,利用loop结束本次循环
m = sqrt(i + 1);
for(j = 3; j <= m; j = j + 2){
if(i % j == 0){
i++;
loop = 1;
break;
}
}
//辅助跳出循环
if(loop){
loop = 0;
continue;
}
//对满足条件的,couple+1
if(i - n == 2){
couple++;
}
n = i;
i++;
}
printf("%d
",couple);
return 0;
}