0

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!

Antony
  • 14,900
  • 10
  • 46
  • 74
PinPiguin
  • 457
  • 2
  • 7
  • 19
  • 2
    What is unclear about the error message? – PeeHaa Apr 01 '13 at 15:26
  • Your problem is that the processing required here is huge. Essentially, you're looping 5,999,994 times. Try `set_time_limit()`. – BenM Apr 01 '13 at 15:26
  • http://stackoverflow.com/questions/2202355/set-maximum-execution-time-for-exec-specifically, http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-exceeded, http://php.net/manual/en/function.set-time-limit.php, http://stackoverflow.com/questions/7793217/fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-c-wamp-www-drupal. http://stackoverflow.com/questions/4051107/php-maximum-execution-time-of-30-seconds-exceeded, http://stackoverflow.com/questions/14081116/fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-joomla-solution-wi – PeeHaa Apr 01 '13 at 15:27
  • How many of these do you need? – 000 Apr 01 '13 at 15:28
  • http://stackoverflow.com/questions/9107215/fatal-error-maximum-execution-time-of-30-seconds-exceeded-when-i-execute-daily, http://stackoverflow.com/questions/2131959/fatal-error-maximum-execution-time-of-400-seconds-exceeded, http://stackoverflow.com/questions/4869611/fatal-error-maximum-execution-time-of-0-seconds-exceeded, http://stackoverflow.com/questions/6509980/fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-script – PeeHaa Apr 01 '13 at 15:29
  • See also: http://mywiki.wooledge.org/XyProblem – 000 Apr 01 '13 at 15:37

3 Answers3

1

add or edit this line in your php.ini

max_execution_time = 0

OR just add the following line in the top of your LuckyTicket.php file:

ini_set('max_execution_time', 0);

Abdessamad
  • 88
  • 6
1

You just need to count them? The sum of digits in 1, 2, 3, 4, ..., 999 are triangular numbers:

I did this in javascript just because :)

for (i=0; i<1000; i++) {
    a[Math.floor(i/100)+(Math.floor(i/10)%10)+i%10] ++;
}

[1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1]

Then you just square the numbers to get the total:

1*1 + 3*3 + ... 

s = 0;
for (i=0; i<28; i++) { s += a[i]*a[i] }

= 55252.

Neat!

000
  • 26,951
  • 10
  • 71
  • 101
1

Here you go.

$tickets = array();

for($intitial_number = 0; $intitial_number < 1000000; $intitial_number++) {

    $int = str_pad($intitial_number, 6, '0', STR_PAD_LEFT);
    $split = str_split($int);   

    if(($split[0] + $split[1] + $split[2]) == ($split[3] + $split[4] + $split[5])){
        $tickets[] = $int;
    }
}

print_r($tickets); // List ticket Numbers
echo count($tickets); // Number of lucky tickets.
Adrian
  • 1,046
  • 7
  • 12