Created
December 24, 2015 10:24
-
-
Save kratskij/349e1cf49edf6974a1de 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 | |
$numbers = str_split("0123456789"); | |
$solutions = inspira($numbers); | |
foreach ($solutions as $solution) { | |
echo $solution . "\n"; | |
} | |
echo "Found " . count($solutions) . " solutions\n"; | |
function inspira($numbers, $reducedNumbers = array()) { | |
$return = array(); | |
foreach($numbers as $key => $value) { | |
$copy = $reducedNumbers; | |
$copy[$key] = $value; | |
$tmp = array_diff_key($numbers, $copy); | |
if (count($tmp) == 0) { | |
$a = (array_shift($copy) . array_shift($copy) . array_shift($copy)); | |
$b = (array_shift($copy) . array_shift($copy)); | |
$c = (array_shift($copy) . array_shift($copy) . array_shift($copy)); | |
$d = (array_shift($copy) . array_shift($copy)); | |
if ($a * $b == $c * $d) { | |
$index = [(int)$a, (int)$b, (int)$c, (int)$d]; | |
sort($index); | |
$return[implode("_", $index)] = "$a * $b - $c * $d = 0"; | |
} | |
} else { | |
$return = array_merge($return, inspira($tmp, $copy)); | |
} | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment