Files
test/project_euler/p010_SummationOfPrimes.cpp

49 lines
781 B
C++

/* Check cf5-opt.vim defs.
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
*/
#include <iostream>
#include <vector>
/*
Summation of primes
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.
Solution:
142913828922
*/
typedef std::vector<long long> pt;
pt p;
bool isPrime( long long n )
{
long h = (long long)sqrt(double(n));
for ( pt::iterator i = p.begin(), ie = p.end();
i!=ie && *i<=h;
++i )
if ( !(n%*i) )
return false;
return true;
}
int main ( void )
{
const long long n = 2000000;
long long s = 0;
for ( long long i = 2; i < n; ++ i )
if ( isPrime( i ) )
{
p.push_back( i );
s+=i;
}
std::cout << s << std::endl;
return 0;
}