Prime number

limit = 1000000;
sqLimit = sqrt(limit)+1;

for(i=3;i<=sqLimit;i++)
if(P[i]==0)
for(j=i*i;j<=limit;j=j+i+i)
P[j]=1;

This method take 1/8 time than sib method. If we use this method, we have to remember that no even number is prime without 2. If we want to put all the prime in an array, then

P[0]=2;
n = 1;

for(i=3;i<=limit;i=i+2)
if(P[i]==0)
{
P[n]=i;
n++;
}

Advertisements