Wednesday, March 19, 2014

[UVa] 543 - Goldbache's Conjecture

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
#define PMAX 1000010
bool ver[PMAX];
vector<int> plist;
int sieve(int lim) {
  int i, j, k;
  plist.clear();
  plist.push_back(2);
  for (int i=3 ; i<=lim ; i+=2) {
    if (ver[i]==false) {
      plist.push_back(i);
      for (int j=3 ; i*j<=lim ; j+=2) {
        ver[i*j] = true;
      }
    }
  }
}

int main() {
  sieve(PMAX);
  int n;

  while (scanf("%d",&n)==1) {
    if (n == 0) break; // Program terminated
    int a=-1, b=-1;
    bool found = false;
    for (int i=0 ; i<plist.size() && plist[i]<=n ; i++) {
      a = n-plist[i];
      b = plist[i];
      if (a>1 && b>1 && (a&1) && (b&1) && ver[a] == false && ver[b] == false) {
        found = true;
        break;
      }
    }
    if (found == true) {
      printf("%d = %d + %d\n",n,b,a); 
    } else {
      printf("Goldbach's conjecture is wrong.\n");
    }

  }
  return 0;
}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...