Law of cosines - triples: Difference between revisions

Content added Content deleted
Line 755: Line 755:
60 #@(solve -. _3 ]\ 3 # >:@i.@]) 10000 NB. optional extra credit
60 #@(solve -. _3 ]\ 3 # >:@i.@]) 10000 NB. optional extra credit
18394</lang>
18394</lang>

=={{header|Java}}==
<lang java>
public class LawOfCosines {

public static void main(String[] args) {
generateTriples(13);
generateTriples60(10000);
}
private static void generateTriples(int max) {
for ( int coeff : new int[] {0, -1, 1} ) {
int count = 0;
System.out.printf("Max side length %d, formula: a*a + b*b %s= c*c%n", max, coeff == 0 ? "" : (coeff>0 ? "-" : "+") + " a*b ");
for ( int a = 1 ; a <= max ; a++ ) {
for ( int b = 1 ; b <= a ; b++ ) {
int val = a*a + b*b + coeff*a*b;
int c = (int) (Math.sqrt(val) + .5d);
if ( c > max ) {
break;
}
if ( c*c == val ) {
System.out.printf(" (%d, %d, %d)%n", a, b ,c);
count++;
}
}
}
System.out.printf("%d triangles%n", count);
}
}

private static void generateTriples60(int max) {
int count = 0;
System.out.printf("%nExtra Credit.%nMax side length %d, sides different length, formula: a*a + b*b - a*b = c*c%n", max);
for ( int a = 1 ; a <= max ; a++ ) {
for ( int b = 1 ; b < a ; b++ ) {
int val = a*a + b*b - a*b;
int c = (int) (Math.sqrt(val) + .5d);
if ( c > max ) {
break;
}
if ( c*c == val ) {
count++;
}
}
}
System.out.printf("%d triangles%n", count);
}

}
</lang>
{{out}}
<pre>
Max side length 13, formula: a*a + b*b = c*c
(4, 3, 5)
(8, 6, 10)
(12, 5, 13)
3 triangles
Max side length 13, formula: a*a + b*b + a*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
Max side length 13, formula: a*a + b*b - a*b = c*c
(5, 3, 7)
(8, 7, 13)
2 triangles

Extra Credit.
Max side length 10000, sides different length, formula: a*a + b*b - a*b = c*c
18394 triangles
</pre>

=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang JavaScript>(() => {
<lang JavaScript>(() => {