您的位置:首页 > 编程语言 > Go语言

53-题目1440:Goldbach's Conjecture

2016-03-09 16:38 495 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: