I wrote this code for generating loads of prime numbers.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long unsigned int limit=4294967294;
long unsigned int n,i,flag,j=1;
clrscr();
for(n=2;n<limit;n++)
{
flag=1;
if(n==2)
{
printf("2");
continue;
}
else
{
if(n%2==0)
continue;
}
for(i=3;i<=sqrt(n)+1;i+=2)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
{
j++;
printf("%luth prime is %lu\n",j,n);
}
}
getch();
}
It took 10 minutes on an AMD 64 BIT 2800 with 1 GB of Ram to get 250,000 primes.
Now I know that everytime the prime is outputted to the screen it slows the program, so wanted to store the primes in an array.
I created an array like this:
Code:
long unsigned int n,i,flag,j=1, a[4294967293];
I get an error saying Array should have atleast one value.
If i reduce the array size to say a[10000], it works fine.
What could be the issue?