Project Euler Problem10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.



#include <bits/stdc++.h>
using namespace std;
int main()
{
        int sieve[2000000];

    sieve[0] = 0;
    sieve[1] = 0;


       for(int k=2; k < 2000000; k++)
       {
           sieve[k] = 1;
       }


    for(int i = 2;i<2000000;i++)
    {
        if(sieve[i]==1)
        {
            for(int j = 2*i;j<2000000;j+=i)
            {
                sieve[j] = 0;
            }
        }
    }


 long long int sum = 0;
    for(int i=2; i<2000000; i++)
    {
        if(sieve[i] == 1)
        {
            sum += i;
        }
    }
    cout <<sum <<endl;
}

Comments

Popular posts from this blog

Project Euler Problem7

Project Euler Problem9

Project Euler Problem8