The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula:

           A2 + B2 - 2ABcos(γ) = C2 

Specific angles

For an angle of of   90º   this becomes the more familiar "Pythagoras equation":

           A2 + B2  =  C2           

For an angle of   60º   this becomes the less familiar equation:

           A2 + B2 - AB  =  C2       

And finally for an angle of   120º   this becomes the equation:

           A2 + B2 + AB  =  C2      



Task

   Find all integer solutions (in order) to the three specific cases, distinguishing between each angle being considered.
   Restrain all sides to the integers   1..13   inclusive.
   Show how many results there are for each of the three angles mentioned above.
   Display results on this page.


Note: Triangles with the same length sides but different order are to be treated as the same.

Optional Extra credit

 How many 60° integer triples are there for sides in the range 1..10_000 where the sides are not all of the same length.


Related Task
 Pythagorean triples


See also

 Visualising Pythagoras: ultimate proofs and crazy contortions Mathlogger Video


with javascript_semantics
--constant lim = iff(platform()=JS?13:10000)
constant lim = 10000
--sequence squares = repeat(0,lim*lim)
sequence squares = iff(platform()=JS?{}:repeat(0,lim*lim))
for c=1 to lim do
    squares[c*c] = c
end for
 
function solve(integer angle, maxlen, bool samelen=true)
    sequence res = {}
    for a=1 to maxlen do
        integer a2 = a*a
        for b=a to maxlen do
            integer c2 = a2+b*b
            if angle!=90 then
                if    angle=60  then c2 -= a*b
                elsif angle=120 then c2 += a*b
                else crash("angle must be 60/90/120")
                end if  
            end if
            integer c = iff(c2>length(squares)?0:squares[c2])
            if c!=0 and c<=maxlen then
                if samelen or a!=b or b!=c then
                    res = append(res,{a,b,c})
                end if
            end if
        end for
    end for
    return res
end function
 
procedure show(string fmt,sequence res, bool full=true)
    printf(1,fmt,{length(res),iff(full?sprint(res):"")})
end procedure
 
puts(1,"Integer triangular triples for sides 1..13:\n")
show("Angle  60 has %2d solutions: %s\n",solve( 60,13))
show("Angle  90 has %2d solutions: %s\n",solve( 90,13))
show("Angle 120 has %2d solutions: %s\n",solve(120,13))
--if platform()!=JS then
    show("Non-equilateral angle 60 triangles for sides 1..10000: %d%s\n",solve(60,10000,false),false)
--end if
