Advertisement
Filkolev

Primes In Range

Dec 8th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8"/>
  5.     <title>Primes In Range</title>
  6.     <style>
  7.         body {
  8.             padding: 20px;
  9.         }
  10.  
  11.         div {
  12.             margin-top: 0 auto;
  13.             width: 600px;
  14.         }
  15.         form {
  16.             margin: 0 auto;
  17.             text-align: center;
  18.         }
  19.     </style>
  20. </head>
  21.  
  22. <body>
  23. <form action="04.PrimesInRange.php" method="post">
  24.     <label for="start">Start Number: </label>
  25.     <input type="text" name="start" id="start">
  26.     <label for="end">End number: </label>
  27.     <input type="text" name="end" id="end">
  28.     <input type="submit" name="submit" id="submit">
  29. </form>
  30.  
  31. <div>
  32. <?php
  33.  
  34. function isPrime($num)
  35. {
  36.     if ($num < 2) {
  37.         return false;
  38.     } else if ($num == 2) {
  39.         return true;
  40.     } else {
  41.         for ($divisor = 2; $divisor <= (int)sqrt($num); $divisor++) {
  42.             if ($num % $divisor == 0) {
  43.                 return false;
  44.             }
  45.         }
  46.     }
  47.  
  48.     return true;
  49. }
  50.  
  51.  if (isset($_POST['submit'])
  52.      && $_POST['start'] != ''
  53.      && $_POST['end'] != ''
  54.      && is_numeric($_POST['start'])
  55.      && is_numeric($_POST['end'])) {
  56.  
  57.      $start = $_POST['start'];
  58.      $end = $_POST['end'];
  59.  
  60.      for ($i = $start; $i <= $end; $i++) {
  61.          if (isPrime($i)) {
  62.              echo '<strong>' . $i . '</strong>';
  63.          } else {
  64.              echo $i;
  65.          }
  66.  
  67.          if ($i != $end) {
  68.              echo ", ";
  69.          }
  70.      }
  71.  }
  72. ?>
  73. </div>
  74.  
  75. </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement