I was wondering if anyone could help me a little bit with a problem I encountered on Project Euler. The specific problem at hand is to find the 10,001st prime. I have implemented what I think to be a version of the Sieve of Eratosthenes. When I modify the problem slightly to find the 10th or 20th prime the code works fine. However, when I put in the larger numbers, I'm getting a segmentation error at runtime. Is this just because my computer doesn't have enough memory, or is there a fault in the code? Any help would be much appreciated.
Here's the code I have so far:
#include <stdio.h>
#define SIZE 1000001
int main()
{
int nums[SIZE];
int primes[10000];
int i = 0;
int j = 0;
for (i = 0; i < SIZE - 1; i++)
nums[i] = i;
nums[1] = 0;
for (i = 4; i <= SIZE - 1; i += 2)
nums[i] = 0;
for (i = 3; i < SIZE - 1; i += 2)
{
if( nums[i] != 0)
{
if (nums[i] * nums[i] > SIZE)
break;
for (j = nums[i] + 2; j < SIZE - 1; j += 2)
{
if (nums[j] % nums[i] == 0)
nums[j] = 0;
}
}
}
j = 0;
for (i = 1; i < SIZE; i += 2)
{
if( nums[i] != 0)
{
primes[j] = nums[i];
j++;
}
if (j == 10000)
{
primes[j] = nums[i];
break;
}
}
printf("%d", primes[10000]);
getchar();
return 0;
}
Thanks for the help!