Message Boards Message Boards

0
|
5520 Views
|
10 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to find list that contains different digits.

Posted 9 years ago

Hi All,

I have question. Assume I have 9 digits, i,e, 1,2,...,9. I want 3 subsets such that each 3 digit number contains digits only ones. Ex: {123,456,789},{123,476,589}, {132,976,845},{321,786,594} so on. My attempt was.

n = FromDigits /@Permutations[Range@9, {3}]
Flatten[Table[{n[[i]], n[[j]], n[[k]]}, {i, Length@n}, {j, 
   Length@n}, {k, Length@n}], 2]

But this does not give me what I want.. My another try was:

In[64]:= n1 = FromDigits /@ Permutations[Range[1, 3], {3}]

Out[64]= {123, 132, 213, 231, 312, 321}

In[65]:= n2 = FromDigits /@ Permutations[Range[4, 6], {3}]

Out[65]= {456, 465, 546, 564, 645, 654}

In[66]:= n3 = FromDigits /@ Permutations[Range[7, 9], {3}]

Out[66]= {789, 798, 879, 897, 978, 987}


 Flatten[Table[{n1[[i]], n2[[j]], n3[[k]]}, {i, Length@n1}, {j, 
       Length@n2}, {k, Length@n3}], 2]

This works but does not cover all permutations.. And I tried this one. But still no luck.

In[1]:= n1 = FromDigits /@ Permutations[{6, 2, 4}, {3}]

Out[1]={624, 642, 264, 246, 462, 426}

In[2]:= n2 = FromDigits /@ Permutations[{5, 7, 8}, {3}]

Out[2]= {578, 587, 758, 785, 857, 875}

In[3]:= n3 = FromDigits /@ Permutations[{3, 1, 9}, {3}]

Out[3]= {319, 391, 139, 193, 931, 913}

Flatten[Table[{n1[[i]], n2[[j]], n3[[k]]}, {i, Length@n1}, {j, 
   Length@n2}, {k, Length@n3}], 2]

Any suggestion? Thanks in advance..

POSTED BY: Okkes Dulgerci
10 Replies
Posted 9 years ago
POSTED BY: Okkes Dulgerci
Posted 9 years ago
n = Partition[ FromDigits /@ Partition[Flatten@Permutations@Range@9, 3], 3];

 DeleteDuplicates[Sort /@ n]
POSTED BY: Okkes Dulgerci
Posted 9 years ago

Maybe this is what you are looking for

Partition[FromDigits /@ Partition[Flatten[Permutations[Range[9]]], 3],
  3]
POSTED BY: Paul Cleary
Posted 9 years ago

Yes, I did that way. After I posted my solution I saw your solution, page was open :)

POSTED BY: Okkes Dulgerci
Posted 9 years ago
POSTED BY: Okkes Dulgerci
Posted 9 years ago
POSTED BY: Okkes Dulgerci
Posted 9 years ago
a = Subsets[Range[9], {3}];

b = Subsets[Complement[Range[9], #], {3}] & /@ a;

c = Flatten[MapThread[Tuples[{{#1}, #2}] &, {a, b}], 1];

n = DeleteDuplicates[
   Sort@Append[#, Complement[Range[9], Flatten[#]]] & /@ c];

d = Table[Permutations /@ n[[i]], {i, Length@n}];

m = Flatten[
   Table[Table[{d[[s, 1, i]], d[[s, 2, j]], d[[s, 3, k]]}, {i, 6}, {j,
       6}, {k, 6}], {s, Length@d}], 3];

Length@m

60480

p = Table[FromDigits /@ m[[i]], {i, Length@m}]

This is what I got after I modified your code but still I am not sure this cover all possibilities..

POSTED BY: Okkes Dulgerci
Posted 9 years ago

Thank you both for quick reply! This is awesome..

POSTED BY: Okkes Dulgerci
POSTED BY: Sander Huisman
FromDigits /@ Partition[RandomSample[Range[9]], 3]

yielding

{319, 584, 276}

The power here is doing your randomisation before you cut the data up into substructures.

POSTED BY: David Gathercole
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract