I am trying to make a program which names all prime numbers up to n. When I enter numbers > 2 Million, the program crashes. Can someone help me?
#include <stdio.h>
#include <math.h>
void sieve(unsigned long long int n, char primes[]);
int main()
{
unsigned long long int i, n = 2000000; // find the primes up to 500000
char v[n];
sieve(n, v);
for (i=0;i<n;i++)
if (v[i] == 1)
printf("%I64u\n",i); // this just prints out each value if it's Prime
}
void sieve(unsigned long long int n, char primes[])
{
unsigned long long int i, j;
for (i=0;i<n;i++)
primes[i]=1; // we initialize the sieve list to all 1's (True)
primes[0]=0,primes[1]=0; // Set the first two numbers (0 and 1) to 0 (False)
for (i=2;i<sqrt(n);i++) // loop through all the numbers up to the sqrt(n)
for (j=i*i;j<n;j+=i) // mark off each factor of i by setting it to 0 (False)
primes[j] = 0;
}
Error message:
Process returned -1073741571 (0xC00000FD) execution time : 2.032 s