Created
October 23, 2017 11:57
-
-
Save ArtemioVegas/2da666002b427850f914f8161e663786 to your computer and use it in GitHub Desktop.
Вывести на экран все шестизначные счастливые билеты
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* | |
* Вывести на экран все шестизначные счастливые билеты. | |
* Билет называется счастливым, если сумма первых трех цифр в номере билета равна сумме последних трех цифр. | |
* Найдите количество счастливых билетов и процент от общего числа билетов. | |
* | |
*/ | |
function happyTickets() | |
{ | |
$result = []; | |
for($i=100000;$i<=999999;$i++){ | |
$leftPart = substr( (string)$i , 0, 3 ); | |
$rightPart = substr( (string)$i , 3, 3 ); | |
$leftSumm = $rightSumm = 0; | |
for($j=0;$j<=strlen($leftPart)-1;$j++){ | |
$leftSumm += (int) $leftPart[$j]; | |
$rightSumm += (int) $rightPart[$j]; | |
} | |
if($leftSumm == $rightSumm ){ | |
$result[] = $i; | |
} | |
} | |
return $result ? $result : false; | |
} | |
echo '<pre>'; | |
var_dump(happyTickets()); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment