在 1000000 以内验证哥德巴赫猜想:
筛法求素数;
为了减少空间,可能要用素数定理,但这道题空间足够了;
1WA,太着急了没注意到输入为 0 时结束。
# include <stdio.h> # define MAXN 1000000 int m; int ptable[MAXN+1], p[MAXN/4]; void build_ptable(void); void solve(int n); int main() { int n; build_ptable(); while (~scanf("%d", &n) && n) solve(n); return 0; } void build_ptable(void) { int i, j; m = 0; for (i = 2; i*i <= MAXN; ++i) { if (!ptable[i]) { p[++m] = i; for (j = i+i; j <= MAXN; j += i) ptable[j] = 1; } } } void solve(int n) { int i, x, y; for (i = 1; i <= m; ++i) if (!ptable[n-p[i]]) break; printf("%d = %d + %d\n", n, p[i], n-p[i]); }