Problem
The sum of the primes below is .
Find the sum of all the primes below two million.
Solution
#include <iostream>
#include <vector>
int main(void) {
std::vector<bool> prime(2000001, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= 2000000; ++i) {
if (prime[i]) {
for (int j = i * i; j <= 2000000; j += i) {
prime[j] = false;
}
}
}
long long sum = 0;
for (int i = 1; i <= 2000000; ++i) {
if (prime[i]) {
sum += i;
}
}
std::cout << sum << std::endl;
}