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


var squares13 = {}
var squares10000 = {}
 
var initMaps = Fn.new {
    for (i in 1..13) squares13[i*i] = i
    for (i in 1..10000) squares10000[i*i] = i
}
 
var solve = Fn.new { |angle, maxLen, allowSame|
    var solutions = []
    for (a in 1..maxLen) {
        for (b in a..maxLen) {
            var lhs = a*a + b*b
            if (angle != 90) {
                if (angle == 60) {
                    lhs = lhs - a*b
                } else if (angle == 120) {
                    lhs = lhs + a*b
                } else {
                    Fiber.abort("Angle must be 60, 90 or 120 degrees")
                }
            }
            if (maxLen == 13) {
                var c = squares13[lhs]
                if (c != null) {
                    if (allowSame || a != b || b != c) {
                        solutions.add([a, b, c])
                    }
                }
            } else if (maxLen == 10000) {                 
                var c = squares10000[lhs]
                if (c != null) {
                    if (allowSame || a != b || b != c) {
                        solutions.add([a, b, c])
                    }
                }
            } else {
                Fiber.abort("Maximum length must be either 13 or 10000")
            }
        }
    }
    return solutions
}
 
initMaps.call()
System.write("For sides in the range [1, 13] ")
System.print("where they can all be of the same length:-\n")
var angles = [90, 60, 120]
var solutions = []
for (angle in angles) {
    solutions = solve.call(angle, 13, true)
    System.write("  For an angle of %(angle) degrees")
    System.print(" there are %(solutions.count) solutions, namely:")
    System.print("  %(solutions)")
    System.print()
}
System.write("For sides in the range [1, 10000] ")
System.print("where they cannot ALL be of the same length:-\n")
solutions = solve.call(60, 10000, false)
System.write("  For an angle of 60 degrees")
System.print(" there are %(solutions.count) solutions.")