4
$\begingroup$

When I compute the following expression to find integer solutions of the equation (x^2 + y^2 + z^2 == 14^2)

Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]

Mathematica returns

{{x -> 4, y -> 6, z -> 12}, {x -> 4, y -> 12, z -> 6}, {x -> 6, y -> 4, z -> 12}, 
 {x -> 6, y -> 12, z -> 4}, {x -> 12, y -> 4, z -> 6}, {x -> 12, y -> 6, z -> 4}}

which are permutations of the only solution: x=4, y=6, z=12.

How can I remove other "solutions" by somehow merging the permutations? For me, x, y, and z are equivalent.


I don't want to do the following:

First[Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]]

Because I will also solve:

Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]

which has more than one solution.


Edit (by belisarius)

Is there a way to specify the equivalency to Solve[] or Reduce[] so to spare the results post-processing stage?

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – bbgodfrey
    Commented Feb 3, 2015 at 3:35
  • $\begingroup$ Related: (3554) $\endgroup$
    – Mr.Wizard
    Commented Feb 3, 2015 at 7:25

4 Answers 4

5
$\begingroup$

You can use DeleteDuplicates, DeleteDuplicatesBy (Version 10) or GatherBy as follows:

ddF = DeleteDuplicates[#, Sort[Last /@ #] == Sort[Last /@ #2] &] &;
ddbF = DeleteDuplicatesBy[#,Sort[Last/@#]&]&;
fgbF = First /@ GatherBy[#, Sort[Last /@ #] &] &;

Examples:

sol1 = Solve[ x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];
sol2 = Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];

ddF[sol1]
(* {{x -> 4, y -> 6, z -> 12}} *)

ddF[sol2]
(* {{x -> 2, y -> 6, z -> 52}, {x -> 2, y -> 36, z -> 38}, 
    {x -> 10,  y -> 12, z -> 50}, {x -> 12, y -> 22, z -> 46},
    {x -> 12, y -> 34,  z -> 38}, {x -> 14, y -> 28, z -> 42},
    {x -> 18, y -> 22,  z -> 44}, {x -> 20, y -> 30, z -> 38}} *)

ddbF and fgbF produce the same output.

$\endgroup$
4
$\begingroup$

For the given problem it is better to use PowersRepresentations or IntegerPartitions which not only avoid the problem but are far faster as well:

IntegerPartitions[14^2, {3}, Range[14]^2] // Sqrt
{{12, 6, 4}}
PowersRepresentations[14^2, 3, 2] ~DeleteCases~ {___, 0, ___}
{{4, 6, 12}}

And for your second example:

IntegerPartitions[14^3, {3}, Range[53]^2] // Sqrt
{{52, 6, 2}, {50, 12, 10}, {46, 22, 12}, {44, 22, 18},
 {42, 28, 14}, {38, 36, 2}, {38, 34, 12}, {38, 30, 20}}
PowersRepresentations[14^3, 3, 2] ~DeleteCases~ {___, 0, ___}
{{2, 6, 52}, {2, 36, 38}, {10, 12, 50}, {12, 22, 46},
 {12, 34, 38}, {14, 28, 42}, {18, 22, 44}, {20, 30, 38}}
$\endgroup$
4
$\begingroup$

Your two example equations are symmetric with respect to $x$, $y$, and $z$. The symmetry leads to the redundancy you want to avoid. Rather than calculate all possible redundant solutions and delete duplicates, you could impose an extra constraint, $x\le y\le z$, to eliminate the duplicates. Hence,

Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0 && x <= y <= z, 
      {x, y, z}, Integers][[All, All, 2]]

{{4, 6, 12}}

and

Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0 && x <= y <= z,
      {x, y, z}, Integers][[All, All, 2]]

{{2, 6, 52}, {2, 36, 38}, {10, 12, 50}, {12, 22, 46}, {12, 34, 38},
 {14, 28, 42}, {18, 22, 44}, {20, 30, 38}}

Mr.Wizard's answer, which uses IntegerPartitions and PowersRepresentations, implicitly avoids the redundancy.

$\endgroup$
1
  • $\begingroup$ This is very natural and doesn't involve another function. Thanks! $\endgroup$ Commented Jun 14, 2015 at 1:31
2
$\begingroup$

One way to approach this is to find all the answers, sort the numbers, and then remove the duplicates:

sol = Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];
Union@Map[Sort, sol[[All, All, 2]], 1]

which returns the unique solution for the three variables. For the 14^3 problem, the same procedure returns a collection of distinct answers

sol = Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];
Union@Map[Sort, sol[[All, All, 2]], 1]

{{2, 6, 52}, {2, 36, 38}, {10, 12, 50}, {12, 22, 46}, {12, 34, 38}, 
 {14, 28, 42}, {18, 22, 44}, {20, 30, 38}}
$\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.