3
$\begingroup$

Got motivation from this and I'm trying to do this:

{#1 , #2 , #3, #1 + #2 + #3}

Where #1, #2, #3 are integer numbers from 1 to 6 and the list is arranged so that the sum is increasing order. So the result is something like this or any resentation showing all different possible results or possibilities of the sum is also good.

{1, 1, 1, 3}
{1, 1, 2, 4}
{1, 2, 1, 4}
{2, 1, 1, 4}
{1, 1, 3, 5}
{1, 2, 2, 5}
{1, 3, 1, 5}
{2, 1, 2, 5}
{3, 1, 1, 5}

Any idea how to do this?

$\endgroup$
1
  • $\begingroup$ Try: SortBy[Append[#, Total[#]] & /@ Tuples[Range[6], 3], Last] $\endgroup$ Jul 28, 2022 at 19:37

3 Answers 3

5
$\begingroup$

I think this does it.

Append[#, Total@#] & /@ 
  Flatten[
    Table[
      Permutations/@IntegerPartitions[n, {3}, Range[6]],
     {n, 3, 18}],
   2]
(* {{1, 1, 1, 3}, {2, 1, 1, 4}, {1, 2, 1, 4}, {1, 1, 2, 4},
    {3, 1, 1, 5}, {1, 3, 1, 5}, {1, 1, 3, 5}, {2, 2, 1, 5},
    {2, 1, 2, 5}, {1, 2, 2, 5}, {4, 1, 1, 6}, {1, 4, 1, 6},
    {1, 1, 4, 6}, {3, 2, 1, 6}, {3, 1, 2, 6}, {2, 3, 1, 6},
    {2, 1, 3, 6}, {1, 3, 2, 6}, {1, 2, 3, 6}, {2, 2, 2, 6},
      ... } *)
$\endgroup$
6
  • $\begingroup$ I think it missed some cases, for example the sum to be 4 then it seems like there are 3 cases {1, 1, 2, 4},{1, 2, 1, 4}, {2, 1, 1, 4}. $\endgroup$
    – hana
    Jul 28, 2022 at 19:29
  • $\begingroup$ Ah I see, so order matters. (I can see that from the OP). IntegerPartitions doesn't care about order, but I can quickly add in a call to Permutations to fix this. Hold on, $\endgroup$
    – march
    Jul 28, 2022 at 19:30
  • $\begingroup$ @hana, so permutations are considered different? Append[#, Total @ #] & /@ Flatten[Table[Permutations /@ IntegerPartitions[n, {3}, Range[6]], {n, 3, 16}], 2] $\endgroup$ Jul 28, 2022 at 19:30
  • $\begingroup$ @J.M. yes, that seems work. I want to list all cases to calcualte possibility of each outcome or over-under. Like you have 3 dices and and you want to see all different cases. $\endgroup$
    – hana
    Jul 28, 2022 at 19:33
  • $\begingroup$ @hana I have made the edits. $\endgroup$
    – march
    Jul 28, 2022 at 19:37
5
$\begingroup$

With a slightly different output format:

GroupBy[Tuples[Range[6], 3], Total]
(*    <|3 -> {{1, 1, 1}},
        4 -> {{1, 1, 2}, {1, 2, 1}, {2, 1, 1}},
        5 -> {{1, 1, 3}, {1, 2, 2}, {1, 3, 1}, {2, 1, 2}, {2, 2, 1}, {3, 1, 1}},
        6 -> {{1, 1, 4}, {1, 2, 3}, {1, 3, 2}, {1, 4, 1}, {2, 1, 3}, {2, 2, 2},
              {2, 3, 1}, {3, 1, 2}, {3, 2, 1}, {4, 1, 1}},
        7 -> {{1, 1, 5}, {1, 2, 4}, {1, 3, 3}, {1, 4, 2}, {1, 5, 1}, {2, 1, 4},
              {2, 2, 3}, {2, 3, 2}, {2, 4, 1}, {3, 1, 3}, {3, 2, 2}, {3, 3, 1},
              {4, 1, 2}, {4, 2, 1}, {5, 1, 1}},
        8 -> {{1, 1, 6}, {1, 2, 5}, {1, 3, 4}, {1, 4, 3}, {1, 5, 2}, {1, 6, 1},
              {2, 1, 5}, {2, 2, 4}, {2, 3, 3}, {2, 4, 2}, {2, 5, 1}, {3, 1, 4},
              {3, 2, 3}, {3, 3, 2}, {3, 4, 1}, {4, 1, 3}, {4, 2, 2}, {4, 3, 1},
              {5, 1, 2}, {5, 2, 1}, {6, 1, 1}},
        9 -> {{1, 2, 6}, {1, 3, 5}, {1, 4, 4}, {1, 5, 3}, {1, 6, 2}, {2, 1, 6},
              {2, 2, 5}, {2, 3, 4}, {2, 4, 3}, {2, 5, 2}, {2, 6, 1}, {3, 1, 5},
              {3, 2, 4}, {3, 3, 3}, {3, 4, 2}, {3, 5, 1}, {4, 1, 4}, {4, 2, 3},
              {4, 3, 2}, {4, 4, 1}, {5, 1, 3}, {5, 2, 2}, {5, 3, 1}, {6, 1, 2},
              {6, 2, 1}},
        10 -> {{1, 3, 6}, {1, 4, 5}, {1, 5, 4}, {1, 6, 3}, {2, 2, 6}, {2, 3, 5},
               {2, 4, 4}, {2, 5, 3}, {2, 6, 2}, {3, 1, 6}, {3, 2, 5}, {3, 3, 4},
               {3, 4, 3}, {3, 5, 2}, {3, 6, 1}, {4, 1, 5}, {4, 2, 4}, {4, 3, 3},
               {4, 4, 2}, {4, 5, 1}, {5, 1, 4}, {5, 2, 3}, {5, 3, 2}, {5, 4, 1},
               {6, 1, 3}, {6, 2, 2}, {6, 3, 1}},
        11 -> {{1, 4, 6}, {1, 5, 5}, {1, 6, 4}, {2, 3, 6}, {2, 4, 5}, {2, 5, 4},
               {2, 6, 3}, {3, 2, 6}, {3, 3, 5}, {3, 4, 4}, {3, 5, 3}, {3, 6, 2},
               {4, 1, 6}, {4, 2, 5}, {4, 3, 4}, {4, 4, 3}, {4, 5, 2}, {4, 6, 1},
               {5, 1, 5}, {5, 2, 4}, {5, 3, 3}, {5, 4, 2}, {5, 5, 1}, {6, 1, 4},
               {6, 2, 3}, {6, 3, 2}, {6, 4, 1}},
        12 -> {{1, 5, 6}, {1, 6, 5}, {2, 4, 6}, {2, 5, 5}, {2, 6, 4}, {3, 3, 6},
               {3, 4, 5}, {3, 5, 4}, {3, 6, 3}, {4, 2, 6}, {4, 3, 5}, {4, 4, 4},
               {4, 5, 3}, {4, 6, 2}, {5, 1, 6}, {5, 2, 5}, {5, 3, 4}, {5, 4, 3},
               {5, 5, 2}, {5, 6, 1}, {6, 1, 5}, {6, 2, 4}, {6, 3, 3}, {6, 4, 2},
               {6, 5, 1}},
        13 -> {{1, 6, 6}, {2, 5, 6}, {2, 6, 5}, {3, 4, 6}, {3, 5, 5}, {3, 6, 4},
               {4, 3, 6}, {4, 4, 5}, {4, 5, 4}, {4, 6, 3}, {5, 2, 6}, {5, 3, 5},
               {5, 4, 4}, {5, 5, 3}, {5, 6, 2}, {6, 1, 6}, {6, 2, 5}, {6, 3, 4},
               {6, 4, 3}, {6, 5, 2}, {6, 6, 1}},
        14 -> {{2, 6, 6}, {3, 5, 6}, {3, 6, 5}, {4, 4, 6}, {4, 5, 5}, {4, 6, 4},
               {5, 3, 6}, {5, 4, 5}, {5, 5, 4}, {5, 6, 3}, {6, 2, 6}, {6, 3, 5},
               {6, 4, 4}, {6, 5, 3}, {6, 6, 2}},
        15 -> {{3, 6, 6}, {4, 5, 6}, {4, 6, 5}, {5, 4, 6}, {5, 5, 5}, {5, 6, 4},
               {6, 3, 6}, {6, 4, 5}, {6, 5, 4}, {6, 6, 3}},
        16 -> {{4, 6, 6}, {5, 5, 6}, {5, 6, 5}, {6, 4, 6}, {6, 5, 5}, {6, 6, 4}},
        17 -> {{5, 6, 6}, {6, 5, 6}, {6, 6, 5}},
        18 -> {{6, 6, 6}}|>    *)
$\endgroup$
1
  • $\begingroup$ ncie orgainization as well $\endgroup$
    – hana
    Jul 29, 2022 at 6:22
3
$\begingroup$
Clear["Global`*"]

nmax = 6;

result = 
  SortBy[{Sequence @@ ##, Total@##} & /@ 
  Tuples[Range[nmax], 3], Last];

Short[result, 5]

enter image description here

$\endgroup$

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.