Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $total = 900; // Total numbers you want to check to see if they are a Strobogrammatic Number:
- /*
- * A simple function telling if a number is a prime number or not.
- */
- function isPrime($n) {
- for ($x = 2; $x < $n; $x++) {
- if ($n % $x == 0) {
- return 0;
- }
- }
- return 1;
- }
- /*
- * Determing if the number is a strobogrammatic nuber based on UTF-8 charset.
- * For more information on strobogrammatical numbers visit - https://en.wikipedia.org/wiki/Strobogrammatic_number
- */
- function isStrob($num) {
- $myNumber = str_split($num);
- for ($i = 0; $i <= count($myNumber) / 2; $i++) {
- $c = $myNumber[$i];
- $b = $myNumber[count($myNumber) - 1 - $i];
- if (!isValid($c, $b)) {
- return FALSE;
- }
- }
- return TRUE;
- }
- /*
- * Check the reverse number against the number.
- */
- function isValid($c, $b) {
- switch ($c) {
- case '1':
- return $b == '1';
- case '6':
- return $b == '9';
- case '9':
- return $b == '6';
- case '8':
- return $b == '8';
- case '0':
- return $b == '0';
- default:
- return FALSE;
- }
- }
- /*
- * Display strobogrammatical numbers in a HTML p element.
- */
- function display_strob_numbers(array $strob_numbers) {
- echo "<p>";
- for ($x = 0; $x < count($strob_numbers); $x++) {
- if ($x === count($strob_numbers) - 1) {
- echo $strob_numbers[$x] . "</p>\n";
- } else {
- echo $strob_numbers[$x] . ", ";
- }
- }
- }
- /*
- * Find the strobogrammatic numbers and prime strobogrammatic numbers if prime variable is set to true.
- * Then send the array containing the strob numbers off to the display function.
- */
- function strobogrammatic($total = 10000, $prime = FALSE) {
- for ($i = 0; $i <= $total; $i++) {
- $status = isStrob($i);
- if ($status) {
- if ($prime) {
- $check = isPrime($i);
- if ($check) {
- $strob_numbers[] = $i;
- }
- } else {
- $strob_numbers[] = $i;
- }
- }
- }
- display_strob_numbers($strob_numbers); // Display the numbers inside a HTML p element:
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Test Upside Up</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <style>
- body {
- box-sizing: border-box;
- background-color: orange;
- padding: 0;
- margin: 0;
- }
- div#strobBox {
- display: block;
- width: 100%;
- max-width: 600px;
- height: auto;
- background-color: #fff;
- padding: 20px;
- margin: 20px auto;
- }
- h1 {
- font-family: Palatino, 'Palatino Linotype', 'Palatino LT STD', 'Book Antiqua', Georgia, serif;
- font-size: 2.4em;
- line-height: 1.0;
- color: #000;
- }
- p {
- font-family: Arial, Helvetica, sans-serif;
- font-size: 1.2em;
- line-height: 1.5;
- color: #000;
- }
- </style>
- </head>
- <body>
- <div id="strobBox">
- <h1>Strobogrammatic Numbers</h1>
- <?php
- strobogrammatic($total); // Display all strobogrammatic numbers up to total:
- ?>
- <h1>Strobogrammatic Prime</h1>
- <?php
- strobogrammatic($total * 10, TRUE); // Display all prime strobogrammatic numbers up to total:
- ?>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement