Hi, i am trying to write a program which calculates the sum of the prime numbers below a specific limit using an algorithm called the Sieve of Eratosthenes(Wikipedia it). I am getting an error though when i run my program. It says debug assertion fail and something how the iterator are incompatible. I have worked much with vectors before, any help??
Here is my code:
#include<iostream>
#include<vector>
#include <numeric>
using namespace std;
int main(){
unsigned int j = 10;
vector<unsigned int> NumList;
for (int i=3; i<=j; i+=2) NumList.push_back(i); // populating vector with odd numbers
vector<unsigned int>::iterator it; // creating an iterator to access values in the vector
vector<unsigned int>::iterator p;
unsigned int q = 0;
while(p<NumList.end()){
p = NumList.begin() + q; // sets p to the first number in the vector which is left and has not been tested
for (it = NumList.begin()+1; it < NumList.end(); it++) {
if(*it%*p == 0) NumList.erase(it); // erases numbers of the vector which are multiples of p
}
q++; // interating q
}
cout << std::accumulate( NumList.begin(), NumList.end(), 0 ) << endl; // summing final values left in the array
system("pause");
return 0;
}