POJ 2262 Goldbach's Conjecture 数论

 题意:哥德巴赫猜想:任何两个大于4的偶数都等于两个奇素数之和。输入n, 求 n = a + b, a, b 为奇素数, 若不存在这样的a, b,输出Goldbach's conjecture is wrong。

#include <memory>
#include <iostream>
using namespace std;

int prime[1000000];

int main()
{
	int i, j, n;
	memset(prime,-1,sizeof(prime));
	prime[0] = prime[1] = 0;
	for ( i = 2; i <= 1000; i++ )
	{
		if ( prime[i] )
			for ( j = 2; j*i <= 1000000; j++ )
				prime[j*i] = 0;
	}
	
	while ( cin >> n && n )
	{
		for ( i = 2; i <= n/2; i++ )
			if ( prime[i] && prime[n-i] )
			{cout << n << " = " << i << " + " << n-i << endl; break;}
	}
	return 0;
}



你可能感兴趣的:(POJ 2262 Goldbach's Conjecture 数论)