Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32564 | Accepted: 12542 |
Description
Every even number greater than 4 can be
written as the sum of two odd prime numbers.
8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
Input
Output
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
筛法求素数
#include <iostream> #include <cmath> #include <vector> #include <cstring> using namespace std; #define MAX 1000001 vector<int>primer(MAX); void pri() { for(int i = 1; i < MAX; i++) { primer[i] = 1; } primer[0] = 0; primer[1] = 0; for(int i = 2; i < MAX / 2; i++) { for(int j = 2; i * j < MAX; j++) { primer[i*j] = 0; } } } int main() { int n; bool flag; pri(); while(cin >> n) { flag = false; if(n == 0) break; for(int i = 2; i <= n; i++) { if(primer[i] && primer[n-i]) { cout << n << " = " << i << " + " << n-i << endl; flag = true; break; } } if(!flag) cout << "Goldbach's conjecture is wrong." << endl; } }