Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
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?

share|improve this question
    
Your first 1000 of rep! That many more may follow. –  Sjoerd C. de Vries Sep 10 '12 at 5:19
    
Yeah! congrats! –  belisarius Sep 10 '12 at 5:20
    
@Sjoerd - Whohoo! Party time! :-) Still a long way to 71k I'm afraid :-) –  stevenvh Sep 10 '12 at 5:24
    
@stevenvh grats! wow, I did not realize you were leading on electronics - nice score ;) –  Vitaliy Kaurov Sep 10 '12 at 15:48
1  
@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. –  stevenvh Sep 10 '12 at 16:12

3 Answers 3

up vote 12 down vote accepted

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

share|improve this answer
    
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. –  stevenvh Sep 10 '12 at 5:22
    
@stevenvh You may find the link at the end of my answer useful for the question in your comment above –  belisarius Sep 10 '12 at 5:50
    
I can't upvote again, but I like the scaly plot... looks like some exotic fish skeleton. :) –  J. M. Sep 10 '12 at 16:31
1  
That plot needs to be on some superhero's chest... –  rm -rf Sep 10 '12 at 18:00
1  
For c given the larger number of solutions the larger grains should be, but they aren't. –  Artes Sep 10 '12 at 18:07

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}}
share|improve this answer
    
Your cone is too steep. Some points are falling off :) –  belisarius Sep 10 '12 at 15:35
    
@belisarius The solution of this problem is out of the scope of the question :) –  Artes Sep 10 '12 at 15:39
    
@belisarius Nevertheless I've corrected the plot. –  Artes Sep 12 '12 at 22:49
    
Ok. I will remove my downvote ;) –  belisarius Sep 12 '12 at 22:51

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.