4

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Richard Cox
  • 305
  • 1
  • 8

4 Answers4

5

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.

kglr
  • 394,356
  • 18
  • 477
  • 896
4

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}}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

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.

KennyColnago
  • 15,209
  • 26
  • 62
2

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}}
bill s
  • 68,936
  • 4
  • 101
  • 191