PE46

/* It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2*1^2 15 = 7 + 2*2^2 21 = 3 + 2*3^2 25 = 7 + 2*3^2 27 = 19 + 2*2^2 33 = 31 + 2*1^2 It turns out that the conjecture was false. What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? */ #include <iostream> #include <vector> #include <windows.h> using namespace std; const int nSIZE = 10000; vector<int> prime; bool primes[nSIZE] = {false}; int factor[nSIZE] = {0}; bool IsPrime(long long int n) { if (n==1) return false; if (n==2) return true; if(n%2==0) return false; long long int hd=8; long long int lg=6; long long int lh=8; long long int va=n-1; while(va>=hd) { if(!((va-hd)%lg)) return false; lh+=8; lg+=4; hd+=lh; } return true; } bool IsOddComposite(int n) { for (int i=0; i<nSIZE; i++) { int temp = prime[i]; if (n % temp==0 && n>temp) { return true; } if (n<=temp) { return false; } } return false; } int main() { __int32 startTime = GetTickCount(); for (int i = 1; i<nSIZE; i++) { if (IsPrime(i)) { prime.push_back(i); primes[i] = true; } factor[i] = 2*i*i; } bool get = false; for (int N = 9; ; N+=2) { //cout << "N = " << N <<" OK!" << endl; if (IsOddComposite(N)) { for (int i = 1; i<nSIZE; i++) { if (primes[N-factor[i]]) break; else if (N-factor[i]<0) { get = true; } } if (get) { cout << N <<'/n'; break; } } } __int32 endTime = GetTickCount(); cout << "the running time = " << endTime-startTime << " ms."; }  

你可能感兴趣的:(PE46)