c++新手求助,验证哥德巴赫猜想
一点不剩 2013-11-26 10:42:47 题目要求建立一个判断素数的函数is_prime
我的程序编写后输出数值中还是有非素数,一下子找不出哪里错了,刚学c++,求高手帮我检查下。
#include<iostream>
using namespace std;
bool is_prime(long int a)
{
long int x=1;
for(;x<=(a/2);x++)
{
if (a%x==0)
{
return 1;
break;
}
if (x>=(a/2)) return 0;
else return 1;
}
}
int main()
{
int x,y,z;
cout<<"请输入一个大于2的偶数:";
cin>>x;
if (x%2!=0||x<2)
{
cout<<"输入的数据有误";
}
else
{ cout<<"可以分解为以下两个素数相加的情况:"<<endl;
for (y=2;is_prime(y)&&y>0;y++)
if (x-y>0&&is_prime(x-y))
cout<<y<<" "<<x-y<<endl;
}
return 0;
}