Skip to content

Instantly share code, notes, and snippets.

@akoebbe
Last active November 16, 2015 16:27
Show Gist options
  • Save akoebbe/9b174945119c8c25183a to your computer and use it in GitHub Desktop.
Save akoebbe/9b174945119c8c25183a to your computer and use it in GitHub Desktop.
<?php
pc_permute(array(1, 2, 3, 4, 5, 6, 7, 8, 9));
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
if(check($perms)) {
print implode('',$perms);
exit();
}
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
function check($items) {
if(count($items) == 0) return true;
if(implode('', $items) % count($items) == 0) {
array_pop($items);
return check($items);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment