How to resolve the algorithm Heronian triangles step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Heronian triangles step by step in the Raku programming language
Table of Contents
Problem Statement
Hero's formula for the area of a triangle given the length of its three sides a, b, and c is given by: where s is half the perimeter of the triangle; that is,
Heronian triangles are triangles whose sides and area are all integers.
Note that any triangle whose sides are all an integer multiple of 3, 4, 5; such as 6, 8, 10, will also be a Heronian triangle. Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor of all three sides is 1 (unity). This will exclude, for example, triangle 6, 8, 10.
Show all output here. Note: when generating triangles it may help to restrict
a <= b <= c
{\displaystyle a<=b<=c}
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Heronian triangles step by step in the Raku programming language
Source code in the raku programming language
sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
}
sub heronian-area($a, $b, $c) {
$_ when Int given hero($a, $b, $c).narrow;
}
sub primitive-heronian-area($a, $b, $c) {
heronian-area $a, $b, $c
if 1 == [gcd] $a, $b, $c;
}
sub show(@measures) {
say " Area Perimeter Sides";
for @measures -> [$area, $perim, $c, $b, $a] {
printf "%6d %6d %12s\n", $area, $perim, "$a×$b×$c";
}
}
sub MAIN ($maxside = 200, $first = 10, $witharea = 210) {
my @hh[1000];
my atomicint $i;
(1 .. $maxside).race(:12batch).map: -> $c {
for 1 .. $c -> $b {
for $c - $b + 1 .. $b -> $a {
if primitive-heronian-area($a,$b,$c) -> $area {
@hh[$i⚛++] = [$area, $a+$b+$c, $c, $b, $a];
}
}
}
}
my @h = (@hh.grep: so *).sort;
say "Primitive Heronian triangles with sides up to $maxside: ", +@h;
say "\nFirst $first:";
show @h[^$first];
say "\nArea $witharea:";
show @h.grep: *[0] == $witharea;
}
You may also check:How to resolve the algorithm MD5 step by step in the Crystal programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Befunge programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the Raku programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the BCPL programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Scala programming language