Heronian triangles: Difference between revisions

Content deleted Content added
→‎{{header|Python}}: Missed another import.
Line 29:
 
<small>'''Note''': when generating triangles it may help to restrict <math>a <= b <= c</math></small>
=={{header|Perl 6}}==
<lang perl6>sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
my $a2 = $s * ($s - $a) * ($s - $b) * ($s - $c);
$a2.sqrt;
}
sub heronian-area($a, $b, $c) {
given hero $a, $b, $c { .floor == .ceiling ?? .floor !! 0 }
}
 
sub primitive-heronian-area($a, $b, $c) {
heronian-area $a, $b, $c
if 1 == [gcd] $a, $b, $c;
}
 
sub show {
say " Area Perimeter Sides";
for @_ -> [$area, $perim, $c, $b, $a] {
printf "%6d %6d %12s\n", $area, $perim, "$a×$b×$c";
}
}
sub MAIN ($maxside = 200, $first = 10, $witharea = 210) {
my \h = sort *.fmt('%05d'), gather
for 1 .. $maxside -> $c {
for 1 .. $c -> $b {
for $c - $b + 1 .. $b -> $a {
if primitive-heronian-area($a,$b,$c) -> $area {
take [$area, $a+$b+$c, $c, $b, $a];
}
}
}
}
 
say "Primitive Heronian triangles with sides up to $maxside: ", +h;
 
say "\nFirst $first:";
show h[^$first];
 
say "\nArea $witharea:";
show h.grep: *[0] == $witharea;
}</lang>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
 
First 10:
Area Perimeter Sides
6 12 3×4×5
12 16 5×5×6
12 18 5×5×8
24 32 4×13×15
30 30 5×12×13
36 36 9×10×17
36 54 3×25×26
42 42 7×15×20
60 36 10×13×13
60 40 8×15×17
 
Area 210:
Area Perimeter Sides
210 70 17×25×28
210 70 20×21×29
210 84 12×35×37
210 84 17×28×39
210 140 7×65×68
210 300 3×148×149</pre>
=={{header|Python}}==
<lang python>from math import sqrt