How to resolve the algorithm Law of cosines - triples step by step in the Lua programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Law of cosines - triples step by step in the Lua programming language
Table of Contents
Problem Statement
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: For an angle of of 90º this becomes the more familiar "Pythagoras equation": For an angle of 60º this becomes the less familiar equation: And finally for an angle of 120º this becomes the equation:
Note: Triangles with the same length sides but different order are to be treated as the same.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Law of cosines - triples step by step in the Lua programming language
Source code in the lua programming language
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
You may also check:How to resolve the algorithm Function definition step by step in the Lingo programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Stata programming language
You may also check:How to resolve the algorithm Variable declaration reset step by step in the Go programming language
You may also check:How to resolve the algorithm Soundex step by step in the C++ programming language
You may also check:How to resolve the algorithm Executable library step by step in the REXX programming language