Summation of primes | Project Euler | Problem #10
URL to the problem page: https://projecteuler.net/problem=10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
#include <iostream>
using namespace std;
int main()
{
long long sum = 0, counter, i, j;
for (i = 3; i < 2000000; i += 2) {
counter = 0;
for (j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
counter++;
break;
}
}
if (counter == 0) {
sum += i;
}
}
cout << "Sum of all the primes below 2.000.000 is = " << sum + 2 << endl;
return 0;
Comments
Post a Comment