YavorGrancharov

Prime Numbers from N to 1

Dec 2nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Prime Numbers from N to 1</title>
  6.  
  7. </head>
  8. <body>
  9. <form>
  10.     N: <input type="text" name="num" />
  11.     <input type="submit" />
  12. </form>
  13. <?php
  14. if (isset($_GET['num'])) {
  15.     $num = intval($_GET['num']);
  16.     $primes = [];
  17.     while ($num > 2) {
  18.         $isPrime = true;
  19.         for ($i = 2; $i <= $num  / 2; $i++) {
  20.             if ($num % $i == 0) {
  21.                 $isPrime = false;
  22.                 break;
  23.             }
  24.         }
  25.         if ($isPrime) {
  26.             array_push($primes, $num);
  27.         }
  28.         $num--;
  29.     }
  30.     echo implode(" ", $primes);
  31. }
  32. ?>
  33. </body>
  34. </html>
Add Comment
Please, Sign In to add comment