# Is there a better way : solutions to integer equation

I have the following code which solves for Integers the equation $$a_1+a_2+a_3+a_4+b_1+b_2+b_3+b_4+c_1+c_2+c_3+c_4=tot$$ with $$-4 $$-7 $$-4 Notice that the constraint on b is different

My method for the moment is the following :

1- find the solutions to $$a+b+c=tot$$ with $$-13 $$-25 $$-13 I use this code to do it (forget about the nbLd argument, it's zero for all you care), it gives a List of solutions.

Solutions[tot_, nbLd_: 0(*forget about nbLd, it's zero*)] := Block[{},
Values@
Solve[a + b + c == tot && -13 < a < 13 && -25 + 6*nbLd < b <
25 - 6*nbLd && -13 < c < 13 , {a, b, c}, Integers]];


2- I use these solutions to find the $$\{a_i\},\{b_i\},\{c_i\}$$ using IntegerPartitions and Permutations(see here) as well as Reap and Sow to find all the solutions to the initial equation. (There is some additional formatting done with Flatten). I use this code to do it:

GenNodes[tot_] :=
Block[{partitions = Solutions[tot], temp},
(Partition[Flatten[#], 2]) & /@ (*Formatting line*)
Flatten[Table[{#[[1, j]], #[[2, i]], #[[3, k]]}, {j,
Length[#[[1]]]}, {i, Length[#[[2]]]}, {k,
Length[#[[3]]]}](*with this Table line,
I cobine all the different a_i,b_i,
c_i solutions to get all possible solutions,
I need all permutations*)& /@ (Reap[(
temp = {

IntegerPartitions[#[[1]], {4},
Range[-3, 3]],(*a_i solutions*)

IntegerPartitions[#[[2]], {4},
Range[-6, 6]],(*b_i solutions*)

IntegerPartitions[#[[3]], {4},
Range[-3, 3]]}; (*c_i solutions*)

If[temp[[1]] != {} && temp[[2]] != {} && temp[[3]] != {},
Sow[{Join @@ Permutations /@ temp[[1]],
Join @@ Permutations /@ temp[[2]],
Join @@ Permutations /@ temp[[3]]}],
Unevaluated[Sequence[]]]) & /@ partitions, e][[1]]), 3]];


The final result is for example :

In[82]:= GenNodes[47]

Out[82]= {{{3, 3}, {3, 2}, {6, 6}, {6, 6}, {3, 3}, {3, 3}}, {{3,
3}, {2, 3}, {6, 6}, {6, 6}, {3, 3}, {3, 3}}, {{3, 2}, {3, 3}, {6,
6}, {6, 6}, {3, 3}, {3, 3}}, {{2, 3}, {3, 3}, {6, 6}, {6, 6}, {3,
3}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 5}, {3, 3}, {3, 3}}, {{3,
3}, {3, 3}, {6, 6}, {5, 6}, {3, 3}, {3, 3}}, {{3, 3}, {3, 3}, {6,
5}, {6, 6}, {3, 3}, {3, 3}}, {{3, 3}, {3, 3}, {5, 6}, {6, 6}, {3,
3}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 6}, {3, 3}, {3, 2}}, {{3,
3}, {3, 3}, {6, 6}, {6, 6}, {3, 3}, {2, 3}}, {{3, 3}, {3, 3}, {6,
6}, {6, 6}, {3, 2}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 6}, {2,
3}, {3, 3}}}


Which, hopefully, are all the solutions to the initial equation for $$tot=47$$, formatted in a certain way.

My goal is to find the solutions of this equation for $$tot =32$$, as it is right now, my computer cannot do it. It freezes (I don't really know why, I'm not an expert, I don't understand why it can't just do it slowly, it maybe a memory problem ? but it freezes super fast, so I don't think it is. It would be helpful if someone could explain this to me !).

Question: basically, can you do it ?

Alternative question: Do yo have a better (memory usage wise or speed wise) way to find the solutions ? or to improve any of the steps?

I'll take any advice

Note: Beware, for 32 the number of solutions is huge.

Why not just use Solve straight?

f[tot_Integer] :=
Solve[a1 + a2 + a3 + a4 + b1 + b2 + b3 + b4 + c1 + c2 + c3 + c4 == tot
&& -4 < a1 <= a2 <= a3 <= a4 < 4
&& -7 < b1 <= b2 <= b3 <= b4 < 7
&& -4 < c1 <= c2 <= c3 <= c4 < 4,
{a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4}, Integers]


In this way, only ordered solutions are found and you can permute them at will afterwards.

Example:

f[47]
(*    {{a1 -> 2, a2 -> 3, a3 -> 3, a4 -> 3, b1 -> 6, b2 -> 6, b3 -> 6, b4 -> 6, c1 -> 3, c2 -> 3, c3 -> 3, c4 -> 3},
{a1 -> 3, a2 -> 3, a3 -> 3, a4 -> 3, b1 -> 5, b2 -> 6, b3 -> 6, b4 -> 6, c1 -> 3, c2 -> 3, c3 -> 3, c4 -> 3},
{a1 -> 3, a2 -> 3, a3 -> 3, a4 -> 3, b1 -> 6, b2 -> 6, b3 -> 6, b4 -> 6, c1 -> 2, c2 -> 3, c3 -> 3, c4 -> 3}}    *)


The case you're interested in:

F32 = f[32];
F32 // Length
(*    23978    *)


So there are only 23978 ordered solutions.

To construct all unordered solutions, permute:

F = {{a1, a2, a3, a4}, {b1, b2, b3, b4}, {c1, c2, c3, c4}} /. F32;
G = Flatten[Tuples[Permutations /@ #] & /@ F, 1];
Length[G]
(*    11694943    *)


So there are about 11 million solutions:

G
(*    {{{-3, -3, -1, 3}, {6, 6, 6, 6}, {3, 3, 3, 3}},
{{-3, -3, 3, -1}, {6, 6, 6, 6}, {3, 3, 3, 3}},
{{-3, -1, -3, 3}, {6, 6, 6, 6}, {3, 3, 3, 3}},
...
{{3, 3, 3, 3}, {6, 6, 6, 6}, {0, -1, -2, -1}},
{{3, 3, 3, 3}, {6, 6, 6, 6}, {0, -1, -1, -2}},
{{3, 3, 3, 3}, {6, 6, 6, 6}, {-1, -1, -1, -1}}}    *)

• Cause sometimes I'm dumb
– yfs
Feb 27, 2021 at 20:09
• I Just want to delete my question, it's kind of embarrassing
– yfs
Feb 27, 2021 at 20:11
• No need to delete. Niels Bohr said: An expert is a man who has made all the mistakes which can be made, in a narrow field. Feb 27, 2021 at 20:13