标签:
素数对猜想是一个很重要的数学猜想,最近它的证明也得到了很大突破,彻底解决它应该不会是太遥远的事情了
PAT要求我们找出前N个自然数中,有多少个素数对,那么可以用筛法把前N个素数中的素数都找出来,然后遍历一次数清有多少素数对就可以啦
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> int main(void) { int n; scanf("%d",&n); //筛法,很适合找出前n个数中的所有素数 bool *a = (bool*)malloc((n + 1)*sizeof(bool)); memset(a, true, n + 1); a[0] = a[1] = false; for (int i = 2; i <= sqrt(n) + 1; i++){ if (a[i] == false){ continue; } for (int j = 2; i*j <= n; j++){ a[i*j] = false; } } int count = 0; for (int i = 3; i <= n - 2; i += 2){ if (a[i] == true && a[i + 2] == true){ count++; } } printf("%d",count); return 0; }
标签:
原文地址:http://www.cnblogs.com/zhouyiji/p/4551585.html