Source Code: | Run it! |
<?php //primes.php |©2005 Chuck Bartholomew - All Rights Reserved|
$n=1000; //Upper limit of integers to test
$p[
0]=2; //Prime numbers found
$s[
0]=2*2; //Squares of prime numbers found
$i=
1; //Number of primes found
$a=
3; //Current number being tested
//Find all primes less than $n
while($a<=$n){
  $isPrime=
True;
  $q=
0;
 
while(($s[$q]<=$a) AND ($isPrime)){
   
if($a==($p[$q]*intval(round($a/$p[$q])))){
      $isPrime=
False;
    }
    $q=$q+
1;
  }
 
if($isPrime){
    $p[$i]=$a;
    $s[$i]=$a*$a;
    $i=$i+
1;
  }
  $a=$a+
1;
}
$j=
0;
//Output list of primes found
while($j<$i){
 
echo $p[$j]."<br/>";
  $j=$j+
1;
}
?>