-
Notifications
You must be signed in to change notification settings - Fork 0
/
10b.cpp
38 lines (34 loc) · 878 Bytes
/
10b.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
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/utility.hpp>
#include <cmath>
const int smallest_prime = 2;
const int goal = 2000000;
const int finish = ceil(sqrt(goal));
int main () {
std::list<int> sieve;
std::list<int>::iterator tortoise;
std::list<int>::iterator hare;
std::copy(boost::counting_iterator<int>(smallest_prime),
boost::counting_iterator<int>(goal),
std::back_inserter(sieve));
tortoise = sieve.begin();
while(tortoise != sieve.end()) {
hare = tortoise;
while(++hare != sieve.end()) {
if(*hare % *tortoise == 0) {
--hare;
sieve.erase(boost::next(hare));
}
}
if(*tortoise > finish) {
break;
}
++tortoise;
}
std::cout << accumulate(sieve.begin(), sieve.end(), 0LL) << std::endl;
return 0;
}