-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sieve.cpp
53 lines (46 loc) · 1.1 KB
/
Sieve.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
46
47
48
49
50
51
52
53
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include "../utils/utils.hpp"
using std::endl;
using std::cout;
using std::vector;
using std::sqrt;
/**
* @biref Implements the sieve of Eratosthenes
* @param n positive integers which is larger than 1(n > 1)
* @output res an array of all prime numbers less or equal to n
*/
void sieve(int n, vector<int>& res)
{
if(n < 2)
throw "n should be larger than 1";
vector<int> temp;
for(int i = 2; i <= n; i++)
temp.push_back(i);
for(auto x = temp.begin(); x != temp.begin() + (int)sqrt(n) - 1; x++)
{
if(*x != 0)
{
auto j = temp.begin() + *x * *x - 2;
while(j < temp.end())
{
*j = 0; //mark element as eliminated
j += *x;
}
}
}
for(auto x : temp)
if(x)
res.push_back(x);
}
int main()
{
vector<int> res;
int n = randint(2, 1000);
sieve(n, res);
cout << "prime numbers between 2 and " << n << " are: " << endl;
for(auto x : res)
cout << x << endl;
}