-
Notifications
You must be signed in to change notification settings - Fork 362
/
sieve_of_eratosthenes.hpp
59 lines (47 loc) · 1.64 KB
/
sieve_of_eratosthenes.hpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Sieve of Eratosthenes
---------------------
Given a number N, find all prime numbers up to N (inclusive) using the simple
Sieve of Eratosthenes method, which efficiently finds primes in the order of
10^7. (The efficiency limit is due to poor cache utilisation.)
Time complexity
---------------
O(N * log(log(N))), where N is the number up to which primes have to be found
Space complexity
----------------
O(N), where N is the number up to which primes have to be found
*/
#ifndef SIEVE_OF_ERATOSTHENES_HPP
#define SIEVE_OF_ERATOSTHENES_HPP
#include <cmath>
#include <vector>
using std::vector;
// Determines primes up to primeLimit and returns list of primes in a vector
vector<int> getPrimes(const unsigned int &primeLimit) {
// Check if larger than limit
if (primeLimit > 10000000) {
return vector<int>({});
}
vector<bool> boolPrimes(primeLimit + 1, true); // "sieve" to mark numbers as prime
boolPrimes[0] = boolPrimes[1] = false;
// Determine primes
unsigned int multiple;
for (unsigned int num = 2; num <= sqrt(primeLimit); num++) {
// check if the number is prime
if (boolPrimes[num]) {
for (multiple = num * num; multiple <= primeLimit; multiple += num)
{
boolPrimes[multiple] = false; // mark its multiples as not prime
}
}
}
// Create output vector
vector<int> primes;
for (unsigned int num = 2; num < boolPrimes.size(); num++) {
if (boolPrimes[num]) {
primes.push_back(num);
}
}
return primes;
}
#endif // SIEVE_OF_ERATOSTHENES_HPP