0
$\begingroup$

Question on the below system of equations:

First off, in section 1 below, I had to manually play with that 4th parameter (300) so I can finally see that there are actually 96 sets of solutions. Is there a way that I do not provide that and Mathematica tells me there are 96? If I remove the parameter I only get 1 set. As I increase it, I eventually see that beyond 96, no matter how high I go it stops at 96.

Second, in section 2 below, I see the CountDistinct gives me the total number of sets. But if you pay attention there are only 4 unique sets (3,21,27,29), (9,11,27,33), (7,13,29,31), (11,13,19,37) - But there are 24 permutations for each set, since the 4 vars are interchangeable, hence 24*4=96 total). How can I ask Mathematica to also list these 4 unique sets?

eqn = FullSimplify[{w + x + y + z == 80, w^2 + x^2 + y^2 + z^2 == 2020}]
Table[FindInstance[eqn, {w, x, y, z}, Integers, 300] ]

{{w -> 3, x -> 21, y -> 27, z -> 29}, {w -> 3, x -> 21, y -> 29, 
  z -> 27}, {w -> 3, x -> 27, y -> 21, z -> 29}, {w -> 3, x -> 27, 
  y -> 29, z -> 21}, {w -> 3, x -> 29, y -> 21, z -> 27}, {w -> 3, 
  x -> 29, y -> 27, z -> 21}, {w -> 7, x -> 13, y -> 29, 
  z -> 31}, {w -> 7, x -> 13, y -> 31, z -> 29}, {w -> 7, x -> 29, 
  y -> 13, z -> 31}, {w -> 7, x -> 29, y -> 31, z -> 13}, {w -> 7, 
  x -> 31, y -> 13, z -> 29}, {w -> 7, x -> 31, y -> 29, 
  z -> 13}, {w -> 9, x -> 11, y -> 27, z -> 33}, {w -> 9, x -> 11, 
  y -> 33, z -> 27}, {w -> 9, x -> 27, y -> 11, z -> 33}, {w -> 9, 
  x -> 27, y -> 33, z -> 11}, .....
...(snip)....
  {w -> 33, x -> 27, y -> 9, z -> 11}, {w -> 33, x -> 27, y -> 11, 
  z -> 9}, {w -> 37, x -> 11, y -> 13, z -> 19}, {w -> 37, x -> 11, 
  y -> 19, z -> 13}, {w -> 37, x -> 13, y -> 11, z -> 19}, {w -> 37, 
  x -> 13, y -> 19, z -> 11}, {w -> 37, x -> 19, y -> 11, 
  z -> 13}, {w -> 37, x -> 19, y -> 13, z -> 11}}
CountDistinct[Table[FindInstance[eqn, {w, x, y, z}, Integers, 300] ]]

{96, 4}

-Thanks

$\endgroup$

3 Answers 3

2
$\begingroup$

I see that Solve and Reduce have been mentioned already in the previous answers. Expanding on LouisB's answer, here is another method that would get you the unique sets:

In[71]:= DeleteDuplicates[Sort[#] & /@ Values[sols]]

Out[71]= {{3, 21, 27, 29}, {7, 13, 29, 31}, {9, 11, 27, 33}, {11, 13, 19, 37}}

However, I should mention that DeleteDuplicates is computaionally more expensive than Select.

$\endgroup$
1
  • $\begingroup$ Thanks to all 3 of you! $\endgroup$
    – Steve237
    Commented Dec 21, 2020 at 6:49
2
$\begingroup$
Reduce[{w + x + y + z == 80, 
  w^2 + x^2 + y^2 + z^2 == 2020}, PositiveIntegers]
%//Length

96

Reduce[{w + x + y + z == 80, w^2 + x^2 + y^2 + z^2 == 2020, 
  w >= x >= y >= z}, PositiveIntegers]
% // Length

4

$\endgroup$
1
$\begingroup$

You can also use Solve to get all of the solutions and Length to count the number of solutions.

eqn = FullSimplify[{w + x + y + z == 80, 
                    w^2 + x^2 + y^2 + z^2 == 2020}]

soln = Solve[eqn, {w, x, y, z}, Integers];

Length@soln   (*  96  *)

To see the unique sets, look for the sets that are sorted (ordered), like this

Select[Values@soln, OrderedQ]

(*  {{3, 21, 27, 29}, {7, 13, 29, 31}, 
     {9, 11, 27, 33}, {11, 13, 19, 37}}  *)

If you only want the four ordered sets, add that constraint to your equations like this

eqn = {w + x + y + z == 80,
   w^2 + x^2 + y^2 + z^2 == 2020,
   w <= x <= y <= z};

soln = Solve[eqn, {w, x, y, z}, Integers];

Length@soln  (*  4  *)
$\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.