Consecutive prime sum

Oct 27, 2013 at 2:43pm
I need to find "Which prime, below one-million, can be written as the sum of the most consecutive primes?". My program gives an answer that is very close to the right answer, but is off by 10. What's wrong with my logic?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int is_prime(int number)
{
    int n=2;
    while (n<number) {
        if (number%n==0) return 0;
        n++;
    }
    return number;
}

int main()
{
int x=2;
int sum=0;

while (sum+is_prime(x)<1000000) {sum=sum+is_prime(x);x++;}
cout << sum << endl;
}

Last edited on Oct 27, 2013 at 2:44pm
Topic archived. No new replies allowed.