Jump to content

Need help-Strobogrammatical numbers


adrianpat
 Share

Recommended Posts

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:

 

<html>
<head>
<title>test upsideup </title>
</head>
<body>
<?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 <br>";
  echo "reverse = $num <br>\n";
   }
    
  }
 
//$test = 10.7;
//echo intval($test);
   
   
?>
</body>
</html>

 

Thanks,

Adrian

 

Link to comment
Share on other sites

Here's my attempt

function is_strobo($n)
{
    $sdigits=[ '0' => '0',  '1' => '1',  '6' => '9',  '8' => '8', '9' => '6'  ];
    //      
    // reverse the digits
    //
    $ra = str_split("$n");
    $r = join('', array_reverse($ra));

    //
    // flip the digits
    //
    for ($i=0, $k=strlen($r); $i<$k; $i++)    {
        $d = $r[$i];
        if ( isset($sdigits[$d]) )    {
            $r[$i] = $sdigits[$d];
        } else  {
            return false;
        }
    }

    return $n==$r;                  // return true if it matches the original
}
  • Like 1
Link to comment
Share on other sites

Here's my version -> 

<?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 ($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;
    }
}


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 "<pre>" . print_r($result, 1) . "</pre>";
?>

<!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">
    </head>
    <body>
        <div>Strobogrammatic Numbers</div>
        <?php
        echo "<p>";
        for ($x = 0; $x < count($result); $x++) {
            if ($x === count($result) - 1) {
                echo $result[$x] . ".<p>";
            } else {
                echo $result[$x] . ", ";
            }
        }
        ?>
    </body>
</html>
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.