List of prime numbers between two number


/ Published in: PHP
Save to your folder(s)

List of prime numbers between two number


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * Softafzar.Net | Developers home_
  5.  * http://softafzar.net
  6.  */
  7.  
  8.  
  9. $num1 = 2;
  10. $num2 = 200;
  11.  
  12. $col = 0;
  13. echo "<h1>List of prime numbers from $num1 to $num2 </h1>";
  14. echo '<table width="60%" border="1"><tr>';
  15. foreach ( Prime ( $num1, $num2 ) as $item ) {
  16. if ($col >= 10) {
  17. echo '</tr><tr>';
  18. $col = 0;
  19. }
  20. echo "<td> $item </td>";
  21. $col ++;
  22. }
  23. echo '</tr></table>';
  24.  
  25. function Prime($num1, $num2) {
  26. $prime_numbers = array ();
  27. while ( $num1 < $num2 ) {
  28. $isprime=true;
  29. for($i = 2; $i <= sqrt ( $num1 ); $i ++) {
  30. if ($num1 % $i == 0)
  31. $isprime=false;
  32. }
  33.  
  34. if ( $isprime ) {
  35. $prime_numbers [] = $num1;
  36. }
  37. $num1 ++;
  38. }
  39. return $prime_numbers;
  40. }
  41. ?>

URL: http://www.softafzar.net/thread1575.html/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.