素数猜想对

有定义dn为dn = pn+1-pn,其中pn是第i个素数,显然有d1 = 1,且对于任意的n>1,都有dn是偶数,“素数猜想对”认为“存在无穷多相邻且查为2的素数”
请计算不超过N = 1e5 的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N。

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4
这道题其实没有什么难度,就是判断素数的时候要注意,因为一般的暴力方法明显会造成超时!

#include <iostream>
#include <vector>
#include <cmath>
bool isPrime(int num)
{
     int tmp=sqrt(num);
     for(int i=2;i<=tmp;i++)
        if(num%i==0)
          return 0;
     return 1;
}
int main()
{
    int n;
    std::vector<int> a;
    std::cin >> n;
    a.reserve(n+1);
    for(int i = 2; i <= n; i++)
    {
        if(isPrime(i))
            a.push_back(i);
    }
    int len = a.size();
    int ans = 0;
    for(int j = 0; j < len - 1; j++)
    {
        if(a[j + 1] - a[j] == 2)
           ans++; 
    }
    std::cout << ans;
    return 0;
}
posted @ 2020-07-06 16:23  YIJIE珂  阅读(72)  评论(0编辑  收藏  举报