How to resolve the algorithm Heronian triangles step by step in the Arturo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Heronian triangles step by step in the Arturo 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 Arturo programming language

Source code in the arturo programming language

printTable: function [title, rows][
    print title ++ ":"
    print repeat "=" 60

    prints pad.center "A" 10
    prints pad.center "B" 10
    prints pad.center "C" 10
    prints pad.center "Perimeter" 15
    print pad.center "Area" 15
    print repeat "-" 60

    loop rows 'row [
        prints pad.center to :string row\0 10
        prints pad.center to :string row\1 10
        prints pad.center to :string row\2 10
        prints pad.center to :string row\3 15
        print pad.center to :string row\4 15
    ]
    print ""
]

hero: function [a,b,c][
    s: (a + b + c) // 2
    return sqrt(s * (s-a) * (s-b) * (s-c))
]

heronian?: function [x]->
    and? -> x > 0
         -> x = ceil x

lst: []
mx: 200

loop 1..mx 'c ->
    loop 1..c 'b ->
        loop 1..b 'a [
            area: hero a b c
            if and? [heronian? area] [one? gcd @[a b c]]->
                'lst ++ @[
                    @[a, b, c, a + b + c, to :integer area]
                ]
        ]

print ["Number of Heronian triangles:" size lst]
print ""

lst: arrange lst 'item ->
    (item\4 * 10000) + (item\3 * 100) + max first.n:3 item

printTable "Ordered list of first ten Heronian triangles" first.n: 10 lst
printTable "Ordered list of Heronian triangles with area 210" select lst 'x -> x\4 = 210


  

You may also check:How to resolve the algorithm Pythagorean triples step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Tosh programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Inform 6 programming language
You may also check:How to resolve the algorithm Loops/For step by step in the PL/I programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Batch File programming language