Need help-Strobogrammatic numbers

Hi,

I am a beginner in Php and I need your help for the following problem: I need to write a code to obtain the strobogrammatical numbers until 10000.

I start with the following code but I need your help for the final part:

[php]

test upsideup <?php function reverse_digit($digit){ switch($digit){ case 0: return 0; case 1: return 1; case 6: return 9; case 8: return 8; case 9: return 6; default: return -1; } } function testUSU($number){ $num2 = $number; // current power of 10 $i = 0; $reverse = 0; while ($num2 > 0){ $i = $i + 1; $num2 = $num2 / 10; } $reverse = $number % 10; $reverse = $number % 10; } for ($x = 0; $x < 10; $x++) { $num = reverse_digit($x); if ($num != -1){ echo "digit = $x
"; echo "reverse = $num
\n"; }

}

//$test = 10.7;
//echo intval($test);

?>

[/php]

Thanks,
Adrian

Well the first thing I would do is try to keep most of the PHP above the HTML. Something like this:
[php]<?php

function reverse_digit($digit) {
switch ($digit) {
case 0:
return 0;
case 1:
return 1;
case 6:
return 9;
case 8:
return 8;
case 9:
return 6;
default:
return -1;
}
}

function testUSU($number) {
$num2 = $number;
// current power of 10
$i = 0;
$reverse = 0;
while ($num2 > 0) {
$i = $i + 1;
$num2 = $num2 / 10;
}
$reverse = $number % 10;
$reverse = $number % 10;
}

for ($x = 0; $x < 100; $x++) {
$num = reverse_digit($x);
if ($num != -1) {
echo "digit = $x
";
echo “reverse = $num
\n”;
}
}

//$test = 10.7;
//echo intval($test);
?>

Test Upside Up
TODO write content
[/php]

I don’t know what possessed me to figure this out, but here’s my version of the problem. ;D
[php]<?php

function IsPrime($n) {
for ($x = 2; $x < $n; $x++) {
if ($n % $x == 0) {
return 0;
}
}
return 1;
}

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;
}

function isValid(string $c, string $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;
}
}

function get_strobogrammatic_numbers($total = 10000) {
for ($i = 0; $i <= $total; $i++) {
$status = isStrob($i);
if ($status) {
$strob_numbers[] = $i;
}
}
return $strob_numbers;
}

$result = get_strobogrammatic_numbers(1000);

//echo “

” . print_r($result, 1) . “
”;
?> Test Upside Up
Strobogrammatic Numbers
<?php echo "

"; for ($x = 0; $x < count($result); $x++) { if ($x === count($result) - 1) { echo $result[$x] . ".

"; } else { echo $result[$x] . ", "; } } ?> [/php]

I am running PHP 7 so if you are running anything lower you might have to take out the string in function isValid(string $c, string $b) { for example.

and doing strobogrammatic prime is done by simply doing the following ->
[php]<?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;
    }

/*

  • Determining if the number is a strobogrammatic number 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 “

    ”;
    for ($x = 0; $x < count($strob_numbers); $x++) {
    if ($x === count($strob_numbers) - 1) {
    echo $strob_numbers[$x] . “

    \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:
    }
    ?>
Test Upside Up 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; }

Strobogrammatic Numbers

<?php strobogrammatic($total); // Display all strobogrammatic numbers up to total: ?>

Strobogrammatic Prime

<?php strobogrammatic($total * 10, TRUE); // Display all prime strobogrammatic numbers up to total: ?>
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service