写一个函数验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和,如6=3+3,8=3+5,10=3+7……在主函数输入一个不小于6的偶数N。然后调用函数gotbaha,在gotbaha函数中再调用prime函数,prime函数的作用是判别一个数是否为素数。在godbah函数中输出以下形式的结果:34=3+31。
数学家的想法果然是难以触摸的。。我还真没想过有这些关系。。
- #include <iostream>
- #include<cmath>
- using namespace std;
- int main()
- {void gotbaha(int);
- int prime(int);
- int a;
- cout<<"请输入不小于6的偶数"<<endl;
- cin>>a;
- gotbaha(a);
- return 0;
- }
- int prime(int x)//x为循环猜想值
- {
- double a;
- int t,c=1;
- if(x==1)c=0;
- else
- {
- for(t=3;t<x;t++)
- {
- a=x%t;
- if(a==0)c=0;
- }
- }
- if(c==1)return 1;
- else return 0;
- }
- void gotbaha(int x)//x=输入的偶数
- {
- int b,c;
- for(b=2;b<x/2;b++)
- {
- if(prime(b))
- {
- c=x-b;
- if(prime(c))
- cout<<x<<"="<<b<<"+"<<c<<endl;
- }
- }
- }
转载于:https://blog.51cto.com/flzt5354/513730