Friday, 30 November 2012

PHP - Prime Numbers

<?php
$limit = 150;
for($i=2; $i <= $limit; $i++) {
  $half = round($i/2);
  $count = 0;
  for($j=2; $j<=$half; $j++) {
 if($i%$j == 0) {
  $count++;
 }
  }
  if($count == 0) {
 echo " ".$i;
  }
}
?>

O/P: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149

No comments:

Post a Comment

Thank you so much for providing your valuable feedback. I will will look into them and update my skills & technologies accordingly.