Law of cosines - triples: Difference between revisions

Added Wren
m (C++ - removed redundant test)
(Added Wren)
Line 1,852:
<pre>There are 18394 60° triangles with unequal sides of max size 10000.
 
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<lang ecmascript>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.")</lang>
 
{{out}}
<pre>
For sides in the range [1, 13] where they can all be of the same length:-
 
For an angle of 90 degrees there are 3 solutions, namely:
[[3, 4, 5], [5, 12, 13], [6, 8, 10]]
 
For an angle of 60 degrees there are 15 solutions, namely:
[[1, 1, 1], [2, 2, 2], [3, 3, 3], [3, 8, 7], [4, 4, 4], [5, 5, 5], [5, 8, 7], [6, 6, 6], [7, 7, 7], [8, 8, 8], [9, 9, 9], [10, 10, 10], [11, 11, 11], [12, 12, 12], [13, 13, 13]]
 
For an angle of 120 degrees there are 2 solutions, namely:
[[3, 5, 7], [7, 8, 13]]
 
For sides in the range [1, 10000] where they cannot ALL be of the same length:-
 
For an angle of 60 degrees there are 18394 solutions.
</pre>
 
9,488

edits