PAT (Basic Level) Practice 1007 素数对猜想 (20分) (求素数的函数)

1.题目

让我们定义d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数。显然有d​1​​=1,且对于n>1有d​n​​是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<10​5​​),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

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

输入样例:

20

输出样例:

4

2.代码

#include<iostream>
#include<cmath>
using namespace std;
int count(int x);
int main()
{
	int n, i, j, k=0,a[100];
	cin >> n;
	for (i = 2; i <=n-2; i++)
	{
		if ((count(i) == 1) &&( count(i + 2) == 1))
			k++;
	}
	cout << k << endl;


}
int count(int x)
{
	int i,k, j;
	k = sqrt(x);
	for (i = 2; i <=k; i++)
		if (x%i == 0)
			return 0;
	return 1;


}

 

posted @ 2020-02-12 08:25  Jason66661010  阅读(105)  评论(0编辑  收藏  举报