function doManyTimes($input, $times) { echo '00: ' . $input . ' - ' . strlen($input) . '<br>'; foreach (range(1, $times) as $i) { $input = lookAndSay($input); echo ($i < 10 ? '0' . $i : $i) . ': ' . substr($input, -100) . (strlen($input) > 100 ? '…' : '') . ' - ' . strlen($input) . '<br>'; } if ($i === $times) { echo '<h4>string length: ' . strlen($input) . '</h4>'; } }
exit("Unable to load input file"); } function lookAndSay($number) { $lastDigit = null; $digitCount = 1; $output = ''; for ($d = 0; $d < strlen($number); $d++) { $digit = $number[$d]; if ($digit == $lastDigit) { $digitCount++; continue; } if (!is_null($lastDigit)) { $output .= $digitCount . $lastDigit; } $lastDigit = $digit; $digitCount = 1; } $output .= $digitCount . $lastDigit; return $output; } for ($c = 0; $c < 40; $c++) { $number = lookAndSay($number); } $after40 = strlen($number); for ($c = 0; $c < 10; $c++) { $number = lookAndSay($number); } $after50 = strlen($number); print "\nPart 1: {$after40}\nPart 2: {$after50}\n";
<?php ini_set('memory_limit', '1024M'); $input = '1113122113'; function lookAndSay($input, $times) { for ($i = 0; $i < $times; $i++) { $input = str_split($input); $output = ''; $lastValue = ''; foreach ($input as $key => $value) { if ($value === $lastValue) { continue; } $count = 0; for ($j = $key; $j < count($input); $j++) { if ($input[$j] !== $value) { break; } $count++; } $output .= $count . $value; $lastValue = $value; } $input = $output; } return $input; } echo 'Length of result after 40 passes is ' . strlen(lookAndSay($input, 40)) . PHP_EOL; echo 'Length of result after 50 passes is ' . strlen(lookAndSay($input, 50)) . PHP_EOL;
<?php function lookAndSay($str) { return preg_replace_callback('#(.)\\1*#', function ($matches) { return strlen($matches[0]) . $matches[1]; }, $str); } $num = "1"; foreach (range(1, 10) as $i) { echo $num . "<br/>"; $num = lookAndSay($num); }
<?php function lookAndSay($s) { $r = ''; // возвращаемое значение $m = $s[0]; // подсчитываемый символ $n = 1; // количество обнаруженных $m} for ($i = 1, $j = strlen($s); $i < $j; $i++) { if ($s[$i] == $m) { $n++; } else { $r .= $n . $m; $m = $s[$i]; $n = 1; } } return $r . $n . $m; } for ($i = 0, $s = 1; $i < 10; $i++) { $s = lookAndSay($s); print "{$s}<br>"; }
<?php $say = "1113122113"; for ($x = 0; $x < 50; $x++) { $say = lookAndSay($say); } echo strlen($say) . "\n"; function lookAndSay($str) { $prevDigit = -1; $runs = 0; $out = ''; for ($x = 0; $x < strlen($str); $x++) { $digit = substr($str, $x, 1); if ($prevDigit == -1) { $runs++; $prevDigit = $digit; continue; } if ($prevDigit == $digit) { $runs++; } else { $out .= $runs . $prevDigit; $runs = 1; } $prevDigit = $digit; } $out .= $runs . $prevDigit; return $out; }
<?php include 'header.php'; for ($sequence = SEQUENCE, $i = 0, $loops = 40; $i < $loops; $i++) { $sequence = lookAndSay($sequence); } echo strlen($sequence), PHP_EOL;
$inputLen = strlen($input); $current = ''; $howMany = 1; $output = ''; for ($i = 0; $i < $inputLen; $i++) { if ($current === $input[$i]) { $howMany++; } else { if (!empty($current)) { $output .= strval($howMany) . $current; } $current = $input[$i]; $howMany = 1; } } $output .= strval($howMany) . $current; return $output; } $input = '1113122113'; $nbRotationPart1 = 40; $nbRotationPart2 = 50; for ($i = 0; $i < $nbRotationPart2; $i++) { $input = lookAndSay($input); if ($i === $nbRotationPart1 - 1) { $outputPart1 = $input; } } print 'Part1: ' . strlen($outputPart1); print PHP_EOL; print 'Part2: ' . strlen($input); print PHP_EOL;