VanessaShopping

Primes In Range

Jan 28th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <!--/**
  2. * Created by PhpStorm.
  3. * User: VanessaShopping
  4. * Date: 28-Jan-17
  5. * Time: 9:55 PM
  6. */-->
  7. <!doctype html>
  8. <html lang="en">
  9. <head>
  10.     <meta charset="UTF-8">
  11.     <meta name="viewport"
  12.           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  13.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  14.     <title>Primes in range</title>
  15. </head>
  16. <body>
  17. <form action="" method="get">
  18.     <lable>Starting index:</lable>
  19.     <input type="number" name="startIndex" id="startIndex">
  20.     <lable>End:</lable>
  21.     <input type="number" name="endIndex" id="endIndex">
  22.     <input type="submit" name="sumbit" id="submit"> </br>
  23.     <?php
  24.     if (isset($_GET['startIndex']) && is_numeric($_GET['startIndex']) && $_GET['endIndex'] && is_numeric($_GET['endIndex'])) {
  25.         $start = intval(trim($_GET['startIndex']));
  26.         $end = intval(trim($_GET['endIndex']));
  27.         $print = '';
  28.         for ($i = $start; $i <= $end; $i++) {
  29.             if ($i > 1 && isPrime($i)) {
  30.                 $print .= "<b>{$i}</b>, ";
  31.             } else {
  32.                 $print .= "{$i}, ";
  33.             }
  34.         }
  35.  
  36.         $print = rtrim($print, ', ');
  37. echo $print;
  38.  
  39.  
  40.         function isPrime($i)
  41.         {
  42.             for ($p = 2; $p <= sqrt($i); $p++) {
  43.                 if ($i % $p == 0) {
  44.                     return false;
  45.                 }
  46.             }
  47.             return true;
  48.         }
  49.  
  50.     } ?>
  51. </form>
  52. </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment