53-題目1440:Goldbach's Conjecture

http://ac.jobdu.com/problem.php?pid=1440

題目描述:

Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. 
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

簡而言之,給一個數N([4,2^15]),找出它的素數對,即要滿足P1+P2=N,求N有多少這樣的素數對。

#include<iostream>
#include<fstream>
using namespace std;
#define MAX 105000

int PrimeNum(int n)   //判斷是否是素數,返回1表示是素數,0非素數
{
	int i;
	for (i = 2; i < n; i++)
	{
		if (n % i == 0)
			break;
	}
	if (i == n)
		return 1;
	else
		return 0;
}

int main()
{
	int num, i, count, p1, p2;
	ifstream cin("data.txt");
	while (cin >> num && num != 0)
	{
		count = 0;
		for (i = 3; i <= num / 2; i++)
		{
			p1 = i;
			p2 = num - i;
			if (PrimeNum(p1) && PrimeNum(p2))  //兩個都是素數
			{
				//cout << p1 << " " << p2 << endl;
				count++;
			}
		}
		cout << count << endl;
	}
	system("pause");
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章