Skip to content

Instantly share code, notes, and snippets.

@kratskij
Created December 24, 2015 10:24
Show Gist options
  • Save kratskij/349e1cf49edf6974a1de to your computer and use it in GitHub Desktop.
Save kratskij/349e1cf49edf6974a1de to your computer and use it in GitHub Desktop.
<?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