So, I am trying to solve the problem of 50 project eilers. (So close to level 2: D) It looks like this:
The first 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, which adds to the prime below 100. The longest sum of consecutive primes below one thousand, which adds to the prime, contains 21 members and is equal to 953. Which prime, less than one million can be written as the sum of the most consecutive primes numbers?
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> primes(1000000,true);
primes[0]=false;
primes[1]=false;
for (int n=4;n<1000000;n+=2)
primes[n]=false;
for (int n=3;n<1000000;n+=2){
if (primes[n]==true){
for (int b=n*2;b<100000;b+=n)
primes[b]=false;
}
}
int basicmax,basiccount=1,currentcount,biggermax,biggercount=1,sum=0,basicstart,basicend,biggerstart,biggerend;
int limit=1000000;
for (int start=2;start<limit;start++){
sum=0;
currentcount=0;
for (int basic=start;start<limit&&sum+basic<limit;basic++){
if (primes[basic]==true){
sum+=basic;currentcount++;}
if (primes[sum]&¤tcount>basiccount&&sum<limit)
{basicmax=sum;basiccount=currentcount;basicstart=start;basicend=basic;}
}
if (basiccount>biggercount)
{biggercount=basiccount;biggermax=basicmax;biggerend=basicend;biggerstart=basicstart;}
}
cout<<biggercount<<endl<<biggermax<<endl;
return 0;
}
Basically, it simply creates a vector of all primes up to 1,000,000, and then iterates through them the correct answer. Answer: 997651, and the number should be 543, but my program outputs are 997661 and 546, respectively. What could be wrong?