sheet 5 not tested
This commit is contained in:
parent
9c16d18f8f
commit
64c7aed176
169 changed files with 225337 additions and 0 deletions
77
sheet5/3/main.cpp
Normal file
77
sheet5/3/main.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include <iostream>
|
||||
#include "mayer_primes.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include "timing.h"
|
||||
#include "omp.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
vector<unsigned int> count_goldbach(unsigned int n)
|
||||
{
|
||||
vector<unsigned int> primes = get_primes(n);
|
||||
size_t npr = primes.size();
|
||||
int nthreads = omp_get_max_threads();
|
||||
|
||||
vector<vector<unsigned int>> local_counts(nthreads, vector<unsigned int>(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<unsigned int> 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<unsigned int> counts = count_goldbach(100000);
|
||||
cout << (max_element(counts.begin(), counts.end()) - counts.begin()) << endl;
|
||||
|
||||
//4
|
||||
vector<unsigned int> 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue