已知不小于6的偶数都可以分成两个素数之和。请编写6到100000的所有偶数的分解,若有一个偶数可以分解成多个素数之和,只需写出一种即可。
#include <iostream>
#include <algorithm>
using namespace std;bool isprime(int n)//判断素数{int i = 2;while (i < n){if (n%i == 0)break;i++;}if (i == n)return true;elsereturn false;}int main()
{for (int i = 6; i <= 1000000; i++){if ((i & 0x1) == 0)//判断是否偶数{int left = i >> 1, right = left;if (isprime(left))cout << i << "可以表达为" << left << "+" << right << endl;else{do{left--;} while (!isprime(left));do{right++;} while (!isprime(right));int sum = left + right;while (sum != i){if (sum < i){do{right++;} while (!isprime(right));}else{do{left--;} while (!isprime(left));}sum = left + right;}cout << i << "可以表达为" << left << "+" << right << endl;}}}return 0;
}
我的思路是: