Codes/Volume 01/E0050 Consecutive prime sum.cpp
/*
Statement:
 The prime 41, can be written as the sum of six consecutive primes:

 41 = 2 + 3 + 5 + 7 + 11 + 13
 This is the longest sum of consecutive primes that adds to a prime below one-hundred.

 The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

 Which prime, below one-million, can be written as the sum of the most consecutive primes?
*/
The prime 41, can be written as the sum of six consecutive primes.



The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.


The prime 41 can be written as the sum of six consecutive primes . The longest sum of consecutive primes below one - thousand that adds to a prime contains 21 terms and is equal to 953 .
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 prime 41, can be written as the sum of six consecutive primes:
Statement : The prime 41 can be written as the sum of six consecutive primes :
// oddComposite[i] = whether 2*i+1 is non-prime