Heronian triangles: Difference between revisions

(→‎{{header|Perl 6}}: better integer test)
Line 315:
(7, 65, 68) perim: 140 area: 210
(3, 148, 149) perim: 300 area: 210</pre>
 
=={{header|zkl}}==
{{trans|Python}}
<lang zkl>fcn hero(a,b,c){ //--> area (float)
s,a2:=(a + b + c).toFloat()/2, s*(s - a)*(s - b)*(s - c);
(a2 > 0) and a2.sqrt() or 0.0
}
fcn isHeronian(a,b,c){
A:=hero(a,b,c);
(A>0) and A.modf()[1].closeTo(0.0,1.0e-6) and A //--> area or False
}</lang>
<lang zkl>const MAX_SIDE=200;
heros:=Sink(List);
foreach a,b,c in ([1..MAX_SIDE],[a..MAX_SIDE],[b..MAX_SIDE]){
if(a.gcd(b).gcd(c)==1 and (h:=isHeronian(a,b,c))) heros.write(T(h,a+b+c,a,b,c));
}
// sort by increasing area, perimeter, then sides
heros=heros.close().sort(fcn([(h1,p1,_,_,_)]a,[(h2,p2,_,_,_)]b){
if(h1!=h2) return(h1<h2);
if(p1!=p2) return(p1<p2);
a[-1]<b[-1];
});
 
println("Primitive Heronian triangles with sides up to %d: ".fmt(MAX_SIDE),heros.len());
 
println("First ten when ordered by increasing area, then perimeter,then maximum sides:");
println("Area Perimeter Sides");
heros[0,10].pump(fcn(phabc){ "%3s %8d %3dx%dx%d".fmt(phabc.xplode()).println() });
 
println("\nAll with area 210 subject to the previous ordering:");
println("Area Perimeter Sides");
heros.filter(fcn([(h,_)]){ h==210 })
.pump(fcn(phabc){ "%3s %8d %3dx%dx%d".fmt(phabc.xplode()).println() });</lang>
{{out}}
<pre>
Primitive Heronian triangles with sides up to 200: 517
First ten when ordered by increasing area, then perimeter,then maximum sides:
Area Perimeter Sides
6 12 3x4x5
12 16 5x5x6
12 18 5x5x8
24 32 4x13x15
30 30 5x12x13
36 36 9x10x17
36 54 3x25x26
42 42 7x15x20
60 36 10x13x13
60 40 8x15x17
 
All with area 210 subject to the previous ordering:
Area Perimeter Sides
210 70 17x25x28
210 70 20x21x29
210 84 12x35x37
210 84 17x28x39
210 140 7x65x68
210 300 3x148x149
</pre>
Anonymous user