Can anyone suggest a good way to find permutations of 12 digits, 0 to 4, totalling 24.
I.e. two such permutations:-
{2, 1, 4, 1, 2, 2, 2, 2, 2, 2, 1, 3} // Total
24
{4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0} // Total
24
I am trying this, but it takes too long:-
FromDigits[{4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0}, 5]
244125000
list = {};
Array[If[Total[IntegerDigits[#, 5]] == 24, list = {list, #}] &, 244125000];
Answer
Use the following
IntegerPartitions[24, {12}, {0, 1, 2, 3, 4}]
(* Out = {{4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 3, 1, 0, 0, 0,
0, 0}, {4, 4, 4, 4, 4, 2, 2, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 2, 1,
1, 0, 0, 0, 0}, {4, 4, 4, 4, 4, 1, 1, 1, 1, 0, 0, 0}, {4, 4, 4, 4,
3, 3, 2, 0, 0, 0, 0, 0}, {4, 4, 4, 4, 3, 3, 1, 1, 0, 0, 0, 0}, {4,
4, 4, 4, 3, 2, 2, 1, 0, 0, 0, 0}, {4, 4, 4, 4, 3, 2, 1, 1, 1, 0, 0,
0}, {4, 4, 4, 4, 3, 1, 1, 1, 1, 1, 0, 0}, {4, 4, 4, 4, 2, 2, 2, 2,
0, 0, 0, 0}, {4, 4, 4, 4, 2, 2, 2, 1, 1, 0, 0, 0}, {4, 4, 4, 4, 2,
2, 1, 1, 1, 1, 0, 0}, {4, 4, 4, 4, 2, 1, 1, 1, 1, 1, 1, 0}, {4, 4,
4, 4, 1, 1, 1, 1, 1, 1, 1, 1}.... *)
Comments
Post a Comment