faster prime generating with seive

I use a seive code for generating primes.I dont know if it is fast enough.If anyone have any idea to make my code faster then please help.Or give me a faster code so I can store that for later use.

Thanks in advance.

//function sieve begin:

#define Max 20000000
int prime_int[Max];
bool prime_bool[Max];
void prime_num()
{
    int i,j,root=sqrt(Max);
    for(i=4;i<=Max;i+=2)
    prime_bool[i]=true;
    for(i=3;i<=root;i+=2)
    if(!prime_bool[i])
    {
        for(j=i*i;j<=Max;j+=2*i)
        prime_bool[j]=true;
    }
    prime_int[0]=2;
    for(i=3,j=1;i<Max;i+=2)
    if(!prime_bool[i])prime_int[j++]=i;
}

//function sieve end

here is a fastest method to generate primes… other methods are sieve of eratosthenes.

the second method is easier to understand and implement also :slight_smile:

1 Like

That code is implement of sieve of eratosthenes

Visit this link

well I can not do python but that list helps.Thanks.