Advertisement
Strider64

Strobogrammatic Numbers

Oct 24th, 2017
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2. $total = 900; // Total numbers you want to check to see if they are a Strobogrammatic Number:
  3.  
  4. /*
  5.  * A simple function telling if a number is a prime number or not.
  6.  */
  7. function isPrime($n) {
  8.     for ($x = 2; $x < $n; $x++) {
  9.         if ($n % $x == 0) {
  10.             return 0;
  11.         }
  12.     }
  13.     return 1;
  14. }
  15.  
  16. /*
  17.  * Determing if the number is a strobogrammatic nuber based on UTF-8 charset.
  18.  * For more information on strobogrammatical numbers visit - https://en.wikipedia.org/wiki/Strobogrammatic_number
  19.  */
  20. function isStrob($num) {
  21.     $myNumber = str_split($num);
  22.     for ($i = 0; $i <= count($myNumber) / 2; $i++) {
  23.         $c = $myNumber[$i];
  24.         $b = $myNumber[count($myNumber) - 1 - $i];
  25.         if (!isValid($c, $b)) {
  26.             return FALSE;
  27.         }
  28.     }
  29.     return TRUE;
  30. }
  31. /*
  32.  * Check the reverse number against the number.
  33.  */
  34. function isValid($c, $b) {
  35.     switch ($c) {
  36.         case '1':
  37.             return $b == '1';
  38.         case '6':
  39.             return $b == '9';
  40.         case '9':
  41.             return $b == '6';
  42.         case '8':
  43.             return $b == '8';
  44.         case '0':
  45.             return $b == '0';
  46.         default:
  47.             return FALSE;
  48.     }
  49. }
  50. /*
  51.  * Display strobogrammatical numbers in a HTML p element.
  52.  */
  53. function display_strob_numbers(array $strob_numbers) {
  54.     echo "<p>";
  55.     for ($x = 0; $x < count($strob_numbers); $x++) {
  56.         if ($x === count($strob_numbers) - 1) {
  57.             echo $strob_numbers[$x] . "</p>\n";
  58.         } else {
  59.             echo $strob_numbers[$x] . ", ";
  60.         }
  61.     }
  62. }
  63. /*
  64.  * Find the strobogrammatic numbers and prime strobogrammatic numbers if prime variable is set to true.
  65.  * Then send the array containing the strob numbers off to the display function.
  66.  */
  67. function strobogrammatic($total = 10000, $prime = FALSE) {
  68.     for ($i = 0; $i <= $total; $i++) {
  69.         $status = isStrob($i);
  70.         if ($status) {
  71.             if ($prime) {
  72.                 $check = isPrime($i);
  73.                 if ($check) {
  74.                     $strob_numbers[] = $i;
  75.                 }
  76.             } else {
  77.                 $strob_numbers[] = $i;
  78.             }
  79.         }
  80.     }
  81.     display_strob_numbers($strob_numbers); // Display the numbers inside a HTML p element:
  82. }
  83. ?>
  84. <!DOCTYPE html>
  85. <html lang="en">
  86.     <head>
  87.         <title>Test Upside Up</title>
  88.         <meta charset="UTF-8">
  89.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  90.         <style>
  91.             body {
  92.                 box-sizing: border-box;
  93.                 background-color: orange;
  94.                 padding: 0;
  95.                 margin: 0;
  96.             }
  97.             div#strobBox {
  98.                 display: block;
  99.                 width: 100%;
  100.                 max-width: 600px;
  101.                 height: auto;
  102.                 background-color: #fff;
  103.                 padding: 20px;
  104.                 margin: 20px auto;
  105.             }
  106.             h1 {
  107.                 font-family: Palatino, 'Palatino Linotype', 'Palatino LT STD', 'Book Antiqua', Georgia, serif;
  108.                 font-size: 2.4em;
  109.                 line-height: 1.0;
  110.                 color: #000;
  111.             }
  112.             p {
  113.                 font-family: Arial, Helvetica, sans-serif;
  114.                 font-size: 1.2em;
  115.                 line-height: 1.5;
  116.                 color: #000;
  117.             }
  118.         </style>
  119.     </head>
  120.     <body>
  121.         <div id="strobBox">
  122.             <h1>Strobogrammatic Numbers</h1>
  123.                 <?php
  124.                   strobogrammatic($total); // Display all strobogrammatic numbers up to total:
  125.                 ?>
  126.             <h1>Strobogrammatic Prime</h1>
  127.                 <?php
  128.                     strobogrammatic($total * 10, TRUE); // Display all prime strobogrammatic numbers up to total:
  129.                 ?>
  130.         </div>
  131.     </body>
  132. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement