Law of cosines - triples: Difference between revisions

→‎{{header|Lua}}: added Lua solution
m (→‎{{header|REXX}}: fixed code to insert comma into an integer (not a FP expressed number).)
(→‎{{header|Lua}}: added Lua solution)
Line 1,366:
For an angle of 60 degrees there are 18394 solutions.
</pre>
 
=={{header|Lua}}==
<lang lua>function solve(angle, maxlen, filter)
local squares, roots, solutions = {}, {}, {}
local cos2 = ({[60]=-1,[90]=0,[120]=1})[angle]
for i = 1, maxlen do squares[i], roots[i^2] = i^2, i end
for a = 1, maxlen do
for b = a, maxlen do
local lhs = squares[a] + squares[b] + cos2*a*b
local c = roots[lhs]
if c and (not filter or filter(a,b,c)) then
solutions[#solutions+1] = {a=a,b=b,c=c}
end
end
end
print(angle.."° on 1.."..maxlen.." has "..#solutions.." solutions")
if not filter then
for i,v in ipairs(solutions) do print("",v.a,v.b,v.c) end
end
end
solve(90, 13)
solve(60, 13)
solve(120, 13)
function fexcr(a,b,c) return a~=b or b~=c end
solve(60, 10000, fexcr) -- extra credit
solve(90, 10000, fexcr) -- more extra credit
solve(120, 10000, fexcr) -- even more extra credit</lang>
{{out}}
<pre>90° on 1..13 has 3 solutions
3 4 5
5 12 13
6 8 10
60° on 1..13 has 15 solutions
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
120° on 1..13 has 2 solutions
3 5 7
7 8 13
60° on 1..10000 has 18394 solutions
90° on 1..10000 has 12471 solutions
120° on 1..10000 has 10374 solutions</pre>
 
=={{header|Nim}}==
Anonymous user