在 1742 年, Christian Goldbach ,一个德国的业余数学家,给 Leonhard Euler 写信,在信中提出如下猜想(哥德巴赫猜想):
• 每个大于 4 的偶数可以写成两个奇素数的和。例如: 8=3+5 , 3 和 5都是奇素数;而 20=3+17=7+13 ; 42=5+37=11+31=13+29=19+23 。
• 现在哥德巴赫猜想仍然没有被证明是否正确。现在请您证明对所有小于一百万的偶数,哥德巴赫猜想成立。
输入格式:
输入包含一个或多个测试用例。每个测试用例给出一个偶整数 n ,6≤n<1000000 。输入以 0 结束。
输出格式:
对每个测试用例,输出形式为 n = a + b 的一行,其中 a 和 b 是奇素数,数字和操作符要用一个空格分开,如样例输出所示。如果有多于一对的奇素数的和加起来为 n ,就选择 b – a 最大的一对。如果没有这样的对,输出 "Goldbach's conjecture is wrong." 。
输入样例:
8
20
42
0
输出样例:
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37
#include<bits/stdc++.h>
using namespace std;
int sushu(int a){
int flag=1;
for(int i=2;i<sqrt(a)+1;i++){
if(a%i==0) flag=0;
}
if(flag==1) return 1;//是素数
else return 0;
}
int main(){
int a,p;
cin>>a;
while(a!=0){
cout<<a<<" = ";
for(p=3;p<a;p+=2){
if(sushu(a-p)&&sushu(p)) {
cout<<p<<" + "<<a-p<<endl;
break;
}
}
cin>>a;
}
}
素数判断的时候 若是i<a 则运行超时
改成i<sqrt(a)+1 可