I have a problem that needs to calculate a number of 'Lucky Tickets' where each ticket has 6 numbers, and 'lucky' ticket will have sum of first 3 numbers equal to sum of second 3 numbers. I know that this problem can be solved by using some algebraic formula, but unfortunately I don't know algebra, so I thought of solving it via brute force - make 2 arrays, sum them and compare one to another like this:
<?php
$intitial_number = 999999;
$A = array();
$B = array();
for(;$intitial_number > 0; $intitial_number--) {
$string = (string)$intitial_number;
$len = strlen($string);
for($i = 0; $i < 3; $i++) {
$A[] = $string[$i];
}
for($i = 3; $i < 6; $i++) {
$B[] = $string[$i];
}
if (array_sum($A) == array_sum($B)) echo 'Ok';
}
?>
When I try to run it I get:
Ok
Fatal error: Maximum execution time of 30 seconds exceeded in /Applications/MAMP/htdocs/Exersize/LuckyTicket.php on line 26
What is my mistake? Is it even possible to solve it like this?
Thanks!