Wednesday, 18 May 2011

543-Goldbases conjection

#include<iostream>
#include<stdlib.h>
#define M 1000000
using namespace std;
int genprime();
bool prime[M];
int main()
{
    long n,i,g;
    //freopen("in.txt","r",stdin);
    genprime();
    while(scanf("%ld",&n) && (n!=0))
    {
        g=0;
        for(i=2;i<=n;i++)
            if(prime[i]&&prime[n-i]){g=1;break;}
        if(g)printf("%ld = %ld + %ld\n",n,i,n-i);
        else
            printf("Goldbach's conjecture is wrong.\n");
    }
    exit(0);
}

int genprime()
{
    unsigned long i,j;
    for(i=2;i<M;i++)
        prime[i]=true;
    for(i=2;i<M;i++)
    {
        if(prime[i])
            for(j=2*i;j<M;j+=i)
                prime[j]=false;
    }
    return 0;
}

No comments:

Post a Comment