6
$\begingroup$
Solve[ a^2 + b^2 == c^2 && a < 100 && b < 100 && c < 100, {a, b, c}, Integers]

gives me this result

{{c -> ConditionalExpression[-Sqrt[ a^2 + b^2],
         ((a | b | c) ∈ Integers &&  Sqrt[10000 - a^2] - b > 0  && -99 <= a <= -1) ||
         ((a | b | c) ∈ Integers &&  Sqrt[10000 - a^2] - b > 0  &&   1 <= a <= 99) || 
         ((a | b | c) ∈ Integers &&  Sqrt[10000 - a^2] + b <= 0 && -99 <= a <= -1) ||
         ((a | b | c) ∈ Integers &&  Sqrt[10000 - a^2] + b <= 0 &&   1 <= a <= 99) ||
              ...           ] },   ...  }

where I would expect something like

{{x -> 3, y -> 4, z -> 5}, {x -> 4, y -> 3, z -> 5}, {x -> 5, y -> 12, z -> 13},...}

What am I doing wrong?

$\endgroup$
6
  • $\begingroup$ Your first 1000 of rep! That many more may follow. $\endgroup$ Commented Sep 10, 2012 at 5:19
  • $\begingroup$ Yeah! congrats! $\endgroup$ Commented Sep 10, 2012 at 5:20
  • $\begingroup$ @Sjoerd - Whohoo! Party time! :-) Still a long way to 71k I'm afraid :-) $\endgroup$
    – stevenvh
    Commented Sep 10, 2012 at 5:24
  • $\begingroup$ @stevenvh grats! wow, I did not realize you were leading on electronics - nice score ;) $\endgroup$ Commented Sep 10, 2012 at 15:48
  • 1
    $\begingroup$ @Vitaliy - Thanks. Hey, I'm doing great: another 5 rep, already at 1045!, so 70k by Thanksgiving may be possible after all :-). I'll have to do a lot of studying on Mathematica until then, I'm afraid. $\endgroup$
    – stevenvh
    Commented Sep 10, 2012 at 16:12

3 Answers 3

12
$\begingroup$

This works (it's obvious the same will hold for negative integers):

Reduce[a^2 + b^2 == c^2 && 0 < a < 100 && 0 < b < 100 && 0 < c < 100, {a, b, c}, Integers]

enter image description here

And if you're up for an interesting visualization, here is my take:

ListPlot3D[{a, b, c} /. Solve[a^2 + b^2 == c^2 && 0 < a < 100 && 0 < b < 100 && 
    0 < c < 100, {a, b, c}, Integers], InterpolationOrder -> 0, 
 Mesh -> None, ColorFunction -> "Rainbow", Filling -> Bottom, BoxRatios -> 1]

enter image description here

$\endgroup$
5
  • $\begingroup$ Thanks. Also seems to work with Solve if I include the lower limits of 0. Any reason why I should use Reduce instead of Solve, other than the different output format? They seem equally fast. $\endgroup$
    – stevenvh
    Commented Sep 10, 2012 at 5:22
  • $\begingroup$ @stevenvh You may find the link at the end of my answer useful for the question in your comment above $\endgroup$ Commented Sep 10, 2012 at 5:50
  • $\begingroup$ I can't upvote again, but I like the scaly plot... looks like some exotic fish skeleton. :) $\endgroup$ Commented Sep 10, 2012 at 16:31
  • 1
    $\begingroup$ That plot needs to be on some superhero's chest... $\endgroup$
    – rm -rf
    Commented Sep 10, 2012 at 18:00
  • 1
    $\begingroup$ For c given the larger number of solutions the larger grains should be, but they aren't. $\endgroup$
    – Artes
    Commented Sep 10, 2012 at 18:07
6
$\begingroup$

Bound the variables from both sides if you want to get explicit results

s = Solve[a^2+b^2==c^2&& -100 < a < 100&& -100 < b < 100&& -100 < c < 100, {a,b,c}, Integers]
(* -> {{a -> -99, b -> 0, c -> -99}, {a -> -98, b -> 0, c -> 98} ... *)
ListPointPlot3D[{a, b, c} /. s]

Mathematica graphics

Or you could reduce the domain to positive integers, taken advantage of the problem being quadratic on all vars:

s = Solve[a^2 + b^2 - c^2 == 0 && 
      0 <= a < 100 && 0 <= b < 100 &&  0 <= c < 100, {a, b, c}, Integers]

ListPointPlot3D[{a, b, c} /. s]

Mathematica graphics

Edit

And you can verify that

2 Sum[SquaresR[2, i^2], {i, 0, 99}] -1 == (2 Length@# -Count[#, 0]) &@Flatten[{a, b, c} /. s]
(* True *)

This is a useful reading when solving Diophantine equations

$\endgroup$
5
$\begingroup$

The result we get is correct and one shouldn't expect it in a different form since there are infinitely many solutions and there is no way to enumerate them all. That's why Mathematica returns the result in a symbolic way. This issue appears a bit more obviously when one looks for all solutions in special subclasses, e.g. solutions of this form :

a == 3k, b == 4k, c == 5k for k < 20 && k ∈ Integers, e.g. k == -10^100000. We have to use Reduce instead of Solve in this case :

Reduce[(3 k)^2 + (4 k)^2 == (5 k)^2 && k < 20, k, Integers]
k ∈ Integers && k <= 19

There are infinitely many solutions, however if we add a lower bound for k we'll get a finite numbers of explicit solutions :

Reduce[(3 k)^2 + (4 k)^2 == (5 k)^2 && 15 < k < 20, k, Integers]
k == 16 || k == 17 || k == 18 || k == 19

Let's visualize an interesting part of the solution space to the general problem :

pts = Solve[{ a^2 + b^2 == c^2,
             -300 < a < 100, -300 < b < 100, -300 < c < 100}, {a, b, c}, Integers];
Length[pts]
2345
Show[ ContourPlot3D[ x^2 + y^2 == z^2, {x, -300, 100}, {y, -300, 100}, {z, -300, 100}, 
                    ContourStyle -> Directive[Orange, Opacity[0.3], Specularity[White, 20]], 
                    MeshFunctions -> {#3 &}, Mesh -> 7, PlotPoints -> 100], 
      ListPointPlot3D[ {a, b, c} /. pts, PlotStyle -> Directive[Darker@Red, PointSize[0.004]]]]

enter image description here

These cones (the upper and a part of the lower) denote all real solutions whereas the red points denote all integer sloutions of the sytem : { a^2 + b^2 == c^2, -300 < a < 100, -300 < b < 100, -300 < c < 100}

One might be also interested in integer solutions modulo 100 to find all solutions of this type : a^2 + b^2 == c^2 + 100 k for k ∈ Integers (i.e. solutions satisfy 0 <= a < 100, 0 <= b < 100, 0 <= c < 100 ), there are many more such solutions ( i.e. 17400) :

Solve[ a^2 + b^2 == c^2, {a, b, c}, Modulus -> 100] // Short[#, 2] &
{{a -> 0, b -> 0, c -> 0},  {a -> 0, b -> 0, c -> 10}, {a -> 0, b -> 0, c -> 20},
 {a -> 0, b -> 0, c -> 30},   <<17393>>,     {a -> 99, b -> 90, c -> 49},
 {a -> 99, b -> 90, c -> 51}, {a -> 99, b -> 90, c -> 99}}
$\endgroup$
4
  • $\begingroup$ Your cone is too steep. Some points are falling off :) $\endgroup$ Commented Sep 10, 2012 at 15:35
  • $\begingroup$ @belisarius The solution of this problem is out of the scope of the question :) $\endgroup$
    – Artes
    Commented Sep 10, 2012 at 15:39
  • $\begingroup$ @belisarius Nevertheless I've corrected the plot. $\endgroup$
    – Artes
    Commented Sep 12, 2012 at 22:49
  • $\begingroup$ Ok. I will remove my downvote ;) $\endgroup$ Commented Sep 12, 2012 at 22:51

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.