I was trying to write a program that calculates primes using the sieve of eratosthenes, and well it works beatifully, except that if I want the prime numbers for anything greater then 500,000 I get an unknown exception. At first I thought I was hitting the limit of int's, but I even tried a long long data type and I still get the same thing? Can anyone help me?
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int max;
cout << "Please enter a max prime number: ";
cin >> max;
int prime[max];
for(int x = 0; x <= max; x++)
{
prime[x] = x;
}
prime[1] = 0;
int curFactor = 2;
while((curFactor * curFactor) <= max)
{
int mark = (curFactor + curFactor);
while(mark <= max)
{
prime[mark] = 0;
mark += curFactor;
}
curFactor++;
while(prime[curFactor] == 0)
curFactor++;
}
for(int x = 0; x <= max; x++)
{
if(prime[x] != 0)
cout << prime[x] << " ";
}
system("PAUSE");
return EXIT_SUCCESS;
}