1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <iostream> using namespace std;
int judge(int);
int main() { cout << "验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和" << endl << endl;
int border, num, front; cout << "请输入欲验证的最大值:"; cin >> border;
for (num = 6; num <= border; num = num + 2) { for (front = 3; front <= num / 2; front = front + 2) { int behind = num - front;
if ((judge(front) == 1) & (judge(behind) == 1)) { cout << num << "=" << front << "+" << behind << endl; } } }
system("pause"); return 0; }
int judge(int tmp) { for (int i = 3; i <= sqrt(tmp); i = i + 2) { if (tmp % i == 0) return 0; }
return 1; }
|