2
$\begingroup$

I have such a list

sets = Select[IntegerDigits /@ (Range[Floor@Sqrt@987654321]^2), 
  DuplicateFreeQ@# && FreeQ[#, 0] &]

I want to pick all subsets of them that non intersect and their union is ${1...9}$. For example:

samples={{{9},{1,3,5,2,7,6,8,4}},{{1},{4},{3,2,9,7,8,5,6}},
  {{1},{4},{9},{8,7,2,3,5,6}},{{1},{9},{2,5},{3,6},{7,8,4}}};

I came up with

n=3;
Select[Subsets[sets, {n}], Sort[Join@@#]==Range[9]&]//AbsoluteTiming

For n>3, it's too slow, Is there a more efficient way?

$\endgroup$

1 Answer 1

2
$\begingroup$
sets = Select[IntegerDigits /@ (Range[Floor@Sqrt@987654321]^2), 
   DuplicateFreeQ@# && FreeQ[#, 0] &];

list = Union[Sort /@ 
  Flatten[Select[Tuples[(d |-> Select[sets, Length[#] == d &]) /@ #], 
      Length[Union[Flatten[#]]] == 9 &] & /@ IntegerPartitions[9], 1]]

Length[list]

{{{1, 3, 9, 8, 5, 4, 2, 7, 6}}, {{1, 5, 2, 8, 4, 3, 7, 6, 9}},(*...*),
{{9}, {8, 1}, {3, 2, 4}, {5, 7, 6}}, {{1}, {9}, {2, 5}, {3, 6}, {7, 8, 4}}}

82

Lists in number form:

Map[FromDigits, list, {2}]

{{139854276}, {152843769}, {157326849}, {215384976}, {245893761}, \
{254817369}, {326597184}, {361874529}, {375468129}, {382945761}, \
{385297641}, {412739856}, {523814769}, {529874361}, {537219684}, \
{549386721}, {587432169}, {589324176}, {597362481}, {615387249}, \
{627953481}, {653927184}, {672935481}, {697435281}, {714653289}, \
{735982641}, {743816529}, {842973156}, {847159236}, {923187456}, {9, 
  13527684}, {9, 34857216}, {9, 65318724}, {9, 73256481}, {9, 
  81432576}, {36, 5184729}, {36, 5948721}, {81, 2537649}, {81, 
  5673924}, {81, 7436529}, {81, 9253764}, {324, 751689}, {576, 
  321489}, {576, 349281}, {576, 381924}, {729, 385641}, {3249, 
  15876}, {4356, 71289}, {8649, 35721}, {1, 4, 3297856}, {1, 4, 
  3857296}, {1, 4, 5827396}, {1, 4, 6385729}, {1, 4, 8567329}, {1, 4, 
  9572836}, {1, 49, 872356}, {1, 64, 537289}, {1, 256, 73984}, {1, 
  625, 73984}, {4, 16, 537289}, {4, 25, 139876}, {4, 25, 391876}, {4, 
  289, 15376}, {9, 324, 15876}, {16, 25, 73984}, {16, 784, 5329}, {25,
   784, 1369}, {25, 784, 1936}, {25, 841, 7396}, {36, 81, 74529}, {36,
   81, 79524}, {36, 729, 5184}, {81, 324, 7569}, {81, 576, 3249}, {81,
   729, 4356}, {361, 529, 784}, {1, 4, 9, 872356}, {1, 36, 529, 
  784}, {4, 25, 81, 7396}, {9, 25, 361, 784}, {9, 81, 324, 576}, {1, 
  9, 25, 36, 784}}
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.