Law of cosines - triples: Difference between revisions

(Ada version)
Line 211:
3 5 7
7 8 13
</pre>
=={{header|AWK}}==
<lang AWK>
# 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])
}
}
</lang>
{{out}}
<pre>
maximum side length: 13
4 3 5
8 6 10
12 5 13
3 triangles, 90 degrees, a*a + b*b = c*c
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 3 7
8 5 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
15 triangles, 60 degrees, a*a + b*b - a*b = c*c
5 3 7
8 7 13
2 triangles, 120 degrees, a*a + b*b + a*b = c*c
 
maximum side length: 1000
881 triangles, 90 degrees, a*a + b*b = c*c
1260 triangles, 60 degrees, a*a + b*b - a*b = c*c
719 triangles, 120 degrees, a*a + b*b + a*b = c*c
</pre>
 
477

edits