PAT- B -1007. Of prime conjecture

topic

Let us define as dn: dn = pn + 1 - pn , where pi is the i-th prime number. Clearly d1 = 1 and for n> 1 there is an even number dn. "Primes to guess" that "there is an infinite number of ⽆ adjacent and poor is a prime number 2."
Now given an arbitrary positive integer N (<105), Calculate the number of prime numbers N does not exceed the full-EMPTY is sufficient to conjecture.

Input formats:

Each test contains an input START the Test Example Using use, gives a positive integer N.

Output formats:

Using the number of prime numbers each representing ⼀ a test output ⾏ Example trekking and not full EMPTY no more than N pairs of foot conjecture.

Sample input:

20

Sample output:

4

solution

The core is to determine a prime number

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int judge(int n)//判断是否是素数 
{
	int flag=0;
	for(int i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		{
			flag=1;
			break;
		} 
	}
	if(flag==1)
		return 0;
	else
		return 1;
}
int main()
{
	vector<int> v;
	int n;
	cin>>n;
	int count=0;
	//求出范围内的素数 
	for(int i=2;i<=n;i++)
	{
		if(judge(i)==1)
			v.push_back(i);//加入动态数组 
	}
	for(int i=1;i<v.size();i++)//计算素数对的个数 
	{
		if(v[i]-v[i-1]==2)
			count++;
	}
	cout<<count;
	return 0;
		
} 
Published 83 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/Yk_0311/article/details/104076211