<?php
//$argv = ['', 556, 25, 75, 9, 4, 2, 9]; 
//$argv = ['', 134, 100, 6, 1, 1, 6, 6 ];
//$argv = ['', 306, 50, 1, 5, 1, 8, 7];
$sum = (int)$argv[1]; 
$input_numbers = array_slice($argv, 2);
$operation = ['+','-','*','/'];

if(sizeof($input_numbers) < 2)
    die();

$best_diff = $sum;
while($best_diff != 0) {
    shuffle($input_numbers);
    $numbers = array_slice($input_numbers, random_int(1, sizeof($input_numbers)));

    $exp = "";
    foreach($numbers as $n){
        $exp .= $n;
        $exp .= $operation[random_int(0,3)];
    }
    $exp = substr($exp, 0, -1);
    $s = eval("return $exp;");

    if(abs($sum - $s) < $best_diff) {
        $best_diff = abs($sum - $s);
        echo "Found: $best_diff =\t$exp\n";
    }
}
