[PHP] Generate 1 To N Prime Numbers in PHP By Sieve Algorithm

<?php

$n=200000;


 $a=array();

 $p=array();




function sieve()

{
   $k=1;
   global $n;
   global $a;
   global $p;

    for($i=1;$i<=$n;$i++)

    {
    $a[$i]=0;
    }

    for($i=1;$i<=$n;$i++)

    {
    $p[$i]=0;
    }

        

$a[0]=1;
$a[1]=1;
    
for($i=2;$i<=$n;$i=$i+2)
{
$a[$i]=1;
}

for($i=3;$i<=sqrt($n);$i=$i+2)
{
for($j=$i*$i; $j<=$n;$j=$j+$i*2)
{
$a[$j]=1;
}
}


for($i=2;$i<=$n;$i++)
{
if($a[$i]== 0)
{
$p[$k]=$i;

$k++;

}

}
}

sieve();


echo("Generated Prime Numbers (1 to n) By Sieve Algorithm <br><br>");


for($m=1;$m<=$n;$m++)

{

   if($p[$m]==0)

   {

      break;

      //error_reporting(0);
   }
   echo($p[$m]."<br>");
   

}






?>

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment