Message Boards Message Boards

0
|
6001 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Filter permutations

hello I need to permute N elements in X groups of elements. the sum of the K elements of each group must be = S
no repetition allowed within groups. ordered or unordered, is the same.

for example N = 90 (from 1 to 90) the sum of the 90 elements is 4095 
90 and 4095 are both divisible by 1, 3, 5, 9, 15, 45 
4095/15 = 273     and      90/15 = 6 
there are 15 groups, each consisting of 6 numbers, the sum of which is 273?
how can I get this?

tnk
POSTED BY: Mutatis Mutandis
4 Replies
Posted 10 years ago
For even numbers of elements in each group (like 6 here) you can just pair the highest with the lowest, second highest with second lowest and take each group of 3 pairs in order.
Join @@@ Partition[Thread@{Range[90/2], Range[90, 90/2 + 1, -1]},
  90/15/2]
{{1, 90, 2, 89, 3, 88}, {4, 87, 5, 86, 6, 85}, {7, 84, 8, 83, 9,
  82}, {10, 81, 11, 80, 12, 79}, {13, 78, 14, 77, 15, 76}, {16, 75,
  17, 74, 18, 73}, {19, 72, 20, 71, 21, 70}, {22, 69, 23, 68, 24,
  67}, {25, 66, 26, 65, 27, 64}, {28, 63, 29, 62, 30, 61}, {31, 60,
  32, 59, 33, 58}, {34, 57, 35, 56, 36, 55}, {37, 54, 38, 53, 39,
  52}, {40, 51, 41, 50, 42, 49}, {43, 48, 44, 47, 45, 46}}
POSTED BY: Michael Hale
Posted 10 years ago
I am not a fan of procedural programming in Mathematica (aka WPL).  But . . . . .
 In[15]:= set = Range[90];
 
 In[16]:= n = 6; sum = 273;
 
 In[17]:= subsets = {};
 
 In[18]:= TimeConstrained[
  Do[
   For[
   try = RandomSample[set, n]; count = 1,
   Total[try] != sum,
   count++,
   try = RandomSample[set, n]
   ];
  subsets = Append[subsets, try];
  set = Complement[set, try];
  , {15}
  ],
30]

In[19]:= subsets

Out[19]= {{44, 72, 18, 74, 25, 40}, {60, 34, 42, 24, 68, 45}, {7, 41,
  27, 82, 36, 80}, {16, 47, 52, 66, 4, 88}, {53, 56, 89, 59, 3,
  13}, {29, 55, 54, 9, 50, 76}, {67, 37, 20, 78, 43, 28}, {10, 32, 49,
   69, 26, 87}, {51, 57, 38, 23, 31, 73}, {65, 6, 22, 46, 70,
  64}, {15, 71, 39, 58, 11, 79}, {75, 86, 2, 21, 8, 81}, {5, 33, 77,
  62, 84, 12}, {1, 61, 85, 90, 17, 19}, {63, 48, 83, 35, 30, 14}}
POSTED BY: David Keith
tnk all
POSTED BY: Mutatis Mutandis
and... if I would get all n=6 sum=273 permutation possible?
POSTED BY: Mutatis Mutandis
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