Skip to content

Instantly share code, notes, and snippets.

@scholtz
Created September 26, 2016 16:20
Show Gist options
  • Select an option

  • Save scholtz/7c571aa628c3b9820076db6554b860ad to your computer and use it in GitHub Desktop.

Select an option

Save scholtz/7c571aa628c3b9820076db6554b860ad to your computer and use it in GitHub Desktop.
<?php
$s = array();
define("SYMBOL",0);
define("OFFSET",1);
for($n = 1;$n<30;$n++){
$symbol = $n;
if($n > 10){
$symbol = chr(54+$n);
}
$s[] = array(SYMBOL=>$symbol,OFFSET=>$n+1);
$i = 0;
$count = count($s)*2;
$CC = 0;
foreach (permutations($s) as $permutation) {
$CC++;
if($CC > 10000000) break;
if($N = getN($permutation)){
$i++;
break;
}
}
if(!$N){$N = "Neexistuje take";}
echo date("H:i:s")." ".$n.":".$CC.":".$N."\n";
}
function permutations(array $elements){
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield array_merge(
array_slice($permutation, 0, $i),
[$elements[0]],
array_slice($permutation, $i)
);
}
}
}
}
function getN($s){
global $count;
$N = array();
for($i = 0;count($s) > 0 && $i<$count;$i++){
if(isset($N[$i])) continue;
$currsymbol = array_pop($s);
if(isset($N[$i+$currsymbol[OFFSET]])) return false;//the binded number is taken
$N[$i] = $currsymbol[SYMBOL];
if($i+$currsymbol[OFFSET]>=$count) return false;
$N[$i+$currsymbol[OFFSET]] = $currsymbol[SYMBOL];
}
ksort($N);
$ret = "";
foreach($N as $v){
$ret.=$v;
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment