Friday, July 18, 2014

UVa - 543 - Goldbach's Conjecture

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#define M 1000000
using namespace std;
bool prime[M];
int main(){
    for(int i = 2; i <= sqrt(M); i++)
        if(prime[i] == 0)
            for(int j = i+i; j < M; j += i)prime[j] = 1;

    int n;
    while(cin >> n){
        if(n == 0)break;
        int m = 0, x, y;
        for(int j = 2; j < n; j++)
            if(!prime[j] && !prime[n-j]){
                    x = j;y = (n-j);
                    m = 1;
                    break;
                }
        if(m)cout << n << " = " << x << " + " << y << endl;
        else cout << "Goldbach's conjecture is wrong." << endl;
    }

    return 0;
}

No comments:

Post a Comment