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:
#include <iostream>
#include <vector>
usingnamespace 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(unsignedint 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?
#include <iostream>
#include <vector>
usingnamespace 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 (unsignedint 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.