adrianpat Posted October 22, 2017 Share Posted October 22, 2017 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 More sharing options...
Barand Posted October 22, 2017 Share Posted October 22, 2017 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 } Link to comment Share on other sites More sharing options...
Gandalf64 Posted October 23, 2017 Share Posted October 23, 2017 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 More sharing options...
Barand Posted October 23, 2017 Share Posted October 23, 2017 FYI, when checking if prime, your for loop only needs to check as far as sqrt(n). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.