孪生素数对
#include <stdio.h>
#include <math.h>
int isPrime (int N);
int main() {
int M;
while (scanf("%d",&M) !=EOF) {
int cishu=0;
int first=0;
int second=0;
for (int j=2;j<=M;j++) {
if (isPrime(j)){
second=first;
first=j;
if((first-second)==2 && (first!=0 && second !=0)){
cishu++;
}
//printf("%d___%d\n",first,second);
}
}
printf("%d",cishu);
}
return 0;
}
int isPrime (int N) {
if (N <=3) {
return N>1;
}
if (N%6 != 1 && N%6 != 5) {
return 0;
}
int ss=(int)sqrt((double)N)+1;
for(int i=5; i <= ss; i+=6) {
if (N%i ==0 || N%(i+2) ==0) {
return 0;
}
}
return 1;
}
原创不易,转载的时候带上帖子地址。