c语言寻找素数数据溢出问题

进行统计不同区间寻找素数的时间,当以50w为起点的时候显示溢出,之前都是正常运行

#include
#include
#include
#define Max 5000
#define Min 500

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int a[Max] = {0};
int i=0;
int j=0;
int count=0;

//基数位置赋予数字
for(i=Min;i<=Max;i++) 
{
    if(i%2!=0)
    {
        a[i]=i;
    }
}

//2是偶数是个例
a[2]=2;

//剩下的基数,基数倍清零
for(i=Min;i<=Max;i++)
{
    if(a[i]!=0)
    {
        j=i+2;
        while(j<=Max)
        {
            if(a[j]%a[i]==0)
            {
                a[j]=0;
            }
            j+=2;
        }
    }
}

//打印素数,也就是非零数字 
for(i=Min;i<=Max;i++)
{
    if(a[i]!=0)
    {
        printf("%d\t",a[i]);
        count++;
        if(count==4)
        {
            printf("\n");
            count=0;
        }
    }
}
return 0;

}

int a[Max] = {0};
不要定义在堆栈上,可以用全局变量或者maloc动态分配
堆栈默认只有2MB
因此50万x4字节=2MB,再大就放不下了。

可以考虑新的方法,例如线性筛,埃氏筛。