How to resolve the algorithm Law of cosines - triples step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Law of cosines - triples step by step in the AWK 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 AWK programming language
Source code in the awk programming language
# syntax: GAWK -f LAW_OF_COSINES_-_TRIPLES.AWK
# converted from C
BEGIN {
description[1] = "90 degrees, a*a + b*b = c*c"
description[2] = "60 degrees, a*a + b*b - a*b = c*c"
description[3] = "120 degrees, a*a + b*b + a*b = c*c"
split("0,1,-1",coeff,",")
main(13,1,0)
main(1000,0,1) # 10,000 takes too long
exit(0)
}
function main(max_side_length,show_sides,no_dups, a,b,c,count,k) {
printf("\nmaximum side length: %d\n",max_side_length)
for (k=1; k<=3; k++) {
count = 0
for (a=1; a<=max_side_length; a++) {
for (b=1; b<=a; b++) {
for (c=1; c<=max_side_length; c++) {
if (a*a + b*b - coeff[k] * a*b == c*c) {
if (no_dups && (a == b || b == c)) {
continue
}
count++
if (show_sides) {
printf(" %d %d %d\n",a,b,c)
}
}
}
}
}
printf("%d triangles, %s\n",count,description[k])
}
}
You may also check:How to resolve the algorithm Dining philosophers step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Arrays step by step in the Boo programming language
You may also check:How to resolve the algorithm Call a function step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Make directory path step by step in the Julia programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Battlestar programming language