Euler 010 c++ Solution

Summation of primes

Problem

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.

Answer: 142913828922

Solution

euler010.cpp

#include <iostream>

#include "sieve_eratos.h"

using namespace std;

int main(int argc, char* argv[] )
{
  CSieveOfEratosthenes* cs = new CSieveOfEratosthenes(20000000);
  cout << "Answer: " << cs->sum(2000000) << endl;
}

The sum method:

  uint64_t sum(int a_max)
  {
    uint64_t total = 0;
    size_t i;
    for( i = 0; i < a_max ; i++){
      if( true == (*m_primes)[i]){
        total += i;
      }
    }
    return total;
  }

See Also