#include #include "mayer_primes.h" #include #include #include "timing.h" #include "omp.h" using namespace std; vector count_goldbach(unsigned int n) { vector primes = get_primes(n); size_t npr = primes.size(); int nthreads = omp_get_max_threads(); vector> local_counts(nthreads, vector(n+1, 0)); #pragma omp parallel { int tid = omp_get_thread_num(); auto &lc = local_counts[tid]; #pragma omp for schedule(static) for (int j = 0; j < (int)npr; ++j) { unsigned int p = primes[j]; for (unsigned int i = j;i < primes.size() && p + primes[i] <= n; ++i) { unsigned int q = primes[i]; unsigned int s = p + q; lc[s] +=1; } } } // combine vector counts(n+1, 0); for (int t = 0; t < nthreads; ++t) for (unsigned int k = 0; k <= n; ++k) counts[k] += local_counts[t][k]; return counts; } int main() { //3 vector counts = count_goldbach(100000); cout << (max_element(counts.begin(), counts.end()) - counts.begin()) << endl; //4 vector nvalues = {10000, 100000, 400000, 1000000, 2000000}; double time; unsigned int n; for(unsigned int i = 0; i< nvalues.size(); i++) { n = nvalues.at(i); tic(); count_goldbach(n); time = toc(); cout << "Time for n=" << n << ": " << time << endl; } return 0; }