/* use seive method to generate the table of prime number reference: http://www.cnblogs.com/xiaobaibuhei/p/3329702.html http://maplewing.blogspot.tw/2011/02/uva543goldbachs-conjecture.html*/ #include <iostream> #include <vector> #include <bitset> #define MAX 1000000 using namespace std; bitset<MAX+1> bs; vector<int> primes; void seive(){ bs.set(); // set all bit to 1 bs[0]=false, bs[1]=false; for (long long int i=2; i<=MAX; ++i){ if (bs[i]==true){ for (long long int j=i*i; j<=MAX; j+=i) bs[j]=false; primes.push_back(i); } } return; } int main(int argc, char *argv[]){ seive(); int n; while(cin>>n && n){ for (int i=0; i<primes.size(); ++i){ if (bs[primes[i]]==true && bs[n-primes[i]]==true){ cout << n << " = " << primes[i] << " + " << n-primes[i] << endl; break; } } } return 0; }
2014年2月6日 星期四
UVA 543 - Goldbach's Conjecture
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言