Goldbach
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
The Goldbach’s Conjecture is stated like the following:
Every even number greater than 4 can be written as the sum of two odd prime numbers.
Your task is now to verify Goldbach\'s Conjecture for all even numbers less than a million.
输入
The input contains multiple test cases. Each test case consisted of an even integer n with 6 < n < 1000000. Input will be terminated by 0.
输出
For every test case, print one line of the form “n = a + b”, a < b, without quotes. Where a and b are odd prime numbers. If there is more than one pair of odd primes, you are to print the pair a, b with the maximized difference b - a. If there are no such pair, print a line saying “Goldbach\'s conjecture is wrong.”.
示例输入
8 20 0
示例输出
8 = 3 + 5 20 = 3 + 17
#include <bits/stdc++.h>
using namespace std;
bool prim(int n)
{
for(int i=2;i*i<=n;i++)
{
if(n%i == 0)
return false;
}
return true;
}
int main()
{
int n;
while(cin>>n && n)
{
for(int i=2;i<=n/2;i++)
{
if(prim(i))
{
int t = n - i;
if(prim(t))
{
cout<<n<<" = "<<i<<" + "<<t<<endl;
break;
}
}
}
}
return 0;
}
本文通过编程实现验证高斯猜想,即每个大于4的偶数都可以表示为两个奇质数之和,对小于百万的偶数进行求解并输出结果。
1071

被折叠的 条评论
为什么被折叠?



