Commit 0e99b5f8 by Andrew Dahl

Solved Problem 10

parent 5206735d
Showing with 39 additions and 0 deletions
Question:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Answer: 142913828922
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool is_prime = true;
int max = 2000000;
long int sum = 0;
vector<long int> primes;
for(int i = 0; i <= max; i++)
primes.push_back(0);
primes[0] = 1;
primes[1] = 1;
primes[2] = 0;
primes[3] = 0;
for(int i = 2; i <= max; i++)
for(int j = i*2; j <= max; j+=i)
primes[j] = 1;
for(int i = 0; i <= max; i++)
if(primes[i] == 0)
sum += i;
cout << sum << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment