Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40944 | Accepted: 15664 |
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
Source
void get_prime() { memset(prime, true, sizeof(prime)); prime[1] = 0; for(int i = 2; i * i <= MAX; i++) if(prime[i]) for(int j = i * i; j <= MAX; j += i) prime[j] = 0; }
void get_prime() { pnum = 0; memset(prime, true, sizeof(prime)); prime[0] = false; prime[1] = false; for(int i = 2; i < MAX; i++) { if(prime[i]) p[pnum ++] = i; for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) break; } } }
#include <cstdio> #include <cstring> int const MAX = 1000005; int p[MAX]; bool prime[MAX]; int pnum; void get_prime() { pnum = 0; memset(prime, true, sizeof(prime)); prime[0] = false; prime[1] = false; for(int i = 2; i < MAX; i++) { if(prime[i]) p[pnum ++] = i; for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) break; } } } // void get_prime() // { // memset(prime, true, sizeof(prime)); // prime[1] = 0; // for(int i = 2; i * i <= MAX; i++) // if(prime[i]) // for(int j = i * i; j <= MAX; j += i) // prime[j] = 0; // } int main() { get_prime(); int n; while(scanf("%d", &n) != EOF && n) { for(int i = 3; i <= n / 2; i += 2) { if(prime[i] && prime[n - i]) { printf("%d = %d + %d\n", n, i, n - i); break; } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2262 Goldbach's Conjecture (求解素数的一般筛和线性筛)
原文:http://blog.csdn.net/tc_to_top/article/details/47261363