Codes/Volume 01/E0010 Summation of primes.cpp
/*
Statement:
 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

 Find the sum of all the primes below two million.
*/


Find the sum of all the primes below two million.
Find the sum of all the primes below two million .
vector<int> primeSieveTill(int maxNum){
    if(maxNum == 1) return vector<int>();
    vector<bool> oddComposite(maxNum/2+1);  
    vector<int> primes{2};
    for(int p = 3; p <= maxNum; p+=2){
        if(oddComposite[p>>1]) continue;
        primes.push_back(p);
        if(maxNum/p < p) continue;
        for(int multiple = p*p; multiple <= maxNum; multiple += p+p)
            oddComposite[multiple/2] = true;
    }
    return primes;
}
vector < int > primeSieveTill ( int maxNum ) { if ( maxNum == 1 ) return vector < int > ( ) ; vector < bool > oddComposite ( maxNum / 2 + 1 ) ; vector < int > primes { 2 } ; for ( int p = 3 ; p <= maxNum ; p += 2 ) { if ( oddComposite [ p >> 1 ] ) continue ; primes . push_back ( p ) ; if ( maxNum / p < p ) continue ; for ( int multiple = p * p ; multiple <= maxNum ; multiple += p + p ) oddComposite [ multiple / 2 ] = true ; } return primes ; }
Statement:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Statement : The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17 .
// oddComposite[i] = whether 2*i+1 is non-prime