Project Euler #10

I was trying out project Euler today and I'm currently working on number 10. The link to the question is: http://projecteuler.net/problem=10. I tried the idea to make a vector containing all prime numbers below 2000000, to see if I know how to use vectors. The code I used is:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //The vector containing the primes
    vector<int> primes;
    //Write the number two to the vector
    primes.push_back(2);
    for(int i=3;i<=2000000;i++)
    {
        //The boolean containing the information if the number is prime or not
        bool isprime = true;
        //Check if the number is prime, if not set isprime to false
        for(unsigned int j=0; j<primes.size() && primes[j]*primes[j] <= i; j++)
        {
            if(i%primes[j] == 0)
            {
                isprime = false;
                break;
            }
        }
        //If the number is a prime, write it to the vector
        if (isprime)
        {
            primes.push_back(i);
        }
    }
    //The integer containing the sum
    int sum = 0;
    //The loop running untill the vector is empty
    while(primes.size()>0)
    {
        //Get the last element in the vector and add it to the sum
        sum+=primes[primes.size()];
        //Delete the number from the vector
        primes.pop_back();
    }
    //Write the sum
    cout << sum << endl;
    //Wait for the user to press a key
    cin.get();
    return 0;
}


The code compiles fine, but the anwser it returns seems to be wrong. I get the anwser 1179908152 while the anwser should be 142913828922. Meaning I missed a lot of numbers. When I try writing the numbers in the vector to the console, the first few primes seem to be correct. The number of elements in the vector after all primes have been added is 148933. Does anyone know what I did wrong in the code?
Last edited on
For one thing primes[primes.size()] is an element one past the end of primes. Presumably you meant to use primes.back().

Perhaps the modification made here will make things clearer to you:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //The vector containing the primes
    vector<int> primes;
    //Write the number two to the vector
    primes.push_back(2);
    for (int i = 3; i <= 2000000; i++)
    {
        //The boolean containing the information if the number is prime or not
        bool isprime = true;
        //Check if the number is prime, if not set isprime to false
        for (unsigned int j = 0; j<primes.size() && primes[j] * primes[j] <= i; j++)
        {
            if (i%primes[j] == 0)
            {
                isprime = false;
                break;
            }
        }
        //If the number is a prime, write it to the vector
        if (isprime)
        {
            primes.push_back(i);
        }
    }
    //The integer containing the sum
    int sum = 0;
    //The loop running untill the vector is empty
    while (primes.size()>0)
    {
        if (sum + primes.back() < sum)
        {
            cerr << "Accumulator (sum) overflow encountered.\n";
            return 0;
        }

        //Get the last element in the vector and add it to the sum
        sum += primes.back();
        //Delete the number from the vector
        primes.pop_back();
    }
    //Write the sum
    cout << sum << endl;
    //Wait for the user to press a key
    cin.get();
    return 0;
}

I'm new to vectors, so I made the mistake of preforming calculations with a number past the vector. After changing the code it gives me the overflow. Is there any type in C++ that would be able to store numbers up to the value I'm trying to reach? Or would I have to try a completely different approach?

EDIT: I tried using a double for sum, it doesn't give me the overflow anymore. It now gives the correct anwser in the format: 1.42914e+011. I think I can manage to write this as the anwser I'm searching for (142913828922) by using cout.precision. Thank you for the help on this code.
Last edited on
Topic archived. No new replies allowed.