-
Notifications
You must be signed in to change notification settings - Fork 0
/
soe.cpp
45 lines (36 loc) · 1.26 KB
/
soe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "misc.hpp"
// simple Sieve of Erathosthenes to find the number of primes up to N
long long sieve(long long N, bool isOpenMP) {
omp_set_num_threads(isOpenMP ? omp_get_num_procs() : 1);
const uint sqrtN = (uint)sqrtl((long double)N);
char* isPrime = new char[N + 1];
#pragma omp parallel for
for (uint i = 0; i <= N; i++) isPrime[i] = 1;
#pragma omp parallel for schedule(dynamic)
for (uint i = 2; i <= sqrtN; i++) {
if (isPrime[i]) {
for (long long j = i * 1LL * i; j <= N; j += i) isPrime[j] = 0;
}
}
long long result = 0;
#pragma omp parallel for reduction(+:result)
for (uint i = 2; i <= N; i++) result += isPrime[i];
delete[] isPrime;
return result;
}
// simple Sieve of Erathosthenes to find odd primes up to N
std::vector < uint > findOddPrimes(uint N) {
char *isPrime = new char[N + 1];
for (uint i = 0; i <= N; i++) isPrime[i] = 1;
for (uint i = 2; i <= N; i++) {
if (isPrime[i]) {
if (i * 1LL * i <= N) {
for (uint j = i * i; j <= N; j += i) isPrime[j] = 0;
}
}
}
std::vector < uint > result;
for (uint i = 3; i <= N; i++) if (isPrime[i]) result.push_back(i);
delete[] isPrime;
return result;
}