Hero's formula for the area of a triangle given the length of its three sides    a,   b,   and   c   is given by:

 



A
=


s
(
s
−
a
)
(
s
−
b
)
(
s
−
c
)


,


{\displaystyle A={\sqrt {s(s-a)(s-b)(s-c)}},}


where   s   is half the perimeter of the triangle; that is,

 



s
=



a
+
b
+
c

2


.


{\displaystyle s={\frac {a+b+c}{2}}.}



Heronian triangles
are triangles whose sides and area are all integers.

 An example is the triangle with sides   3, 4, 5   whose area is   6   (and whose perimeter is   12). 


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.



Task

 Create a named function/method/procedure/... that implements Hero's formula.
 Use the function to generate all the primitive Heronian triangles with sides <= 200.
 Show the count of how many triangles are found.
 Order the triangles by first increasing area, then by increasing perimeter, then by increasing maximum side lengths
 Show the first ten ordered triangles in a table of sides, perimeter, and area.
 Show a similar ordered table for those triangles with area = 210


Show all output here.

Note: when generating triangles it may help to restrict 



a
<=
b
<=
c


{\displaystyle a<=b<=c}



 
include "ConsoleWindow"
 
// Set width of tabs
def tab 10
 
local fn gcd( a as long, b as long )
dim as long result
 
if ( b != 0 )
   result = fn gcd( b, a mod b)
else
   result = abs(a)
end if
end fn = result
 
begin globals
dim as long triangleInfo( 600, 4 )
end globals
 
local fn CalculateHeronianTriangles( numberToCheck as long ) as long
dim as long c, b, a, result, count : count = 0
dim as double s, area
 
for c = 1 to numberToCheck
   for b = 1 to c
      for a = 1 to b
         s = ( a + b + c ) / 2
         area = s * ( s - a ) * ( s - b ) * ( s  - c )
            if area > 0
               area = sqr( area )
               if area = int( area )
                  result = fn gcd( b, c )
                  result = fn gcd( a, result )
                     if result == 1
                        count++
                        triangleInfo( count, 0 ) = a
                        triangleInfo( count, 1 ) = b
                        triangleInfo( count, 2 ) = c
                        triangleInfo( count, 3 ) = 2 * s
                        triangleInfo( count, 4 ) = area
                      end if
                end if
           end if
      next
   next
next
end fn = count
 
dim as long i, k, count
 
count = fn CalculateHeronianTriangles( 200 )
 
print
print "Number of triangles:"; count
print
print "---------------------------------------------"
print "Side A", "Side B", "Side C", "Perimeter", "Area"
print "---------------------------------------------" 
 
// Sort array 
dim as Boolean flips : flips = _true
while ( flips = _true )
   flips = _false
   for i = 1 to count - 1
      if triangleInfo( i, 4 ) > triangleInfo( i + 1, 4 )
         for k = 0 to 4
            swap triangleInfo( i, k ), triangleInfo( i + 1, k )
         next
      flips = _true
      end if
   next
wend
 
// Find first 10 heronian triangles
for i = 1 to 10
   print triangleInfo( i, 0 ), triangleInfo( i, 1 ), triangleInfo( i, 2 ), triangleInfo( i, 3 ), triangleInfo( i, 4 )
next
print
print "Triangles with an area of 210:"
print
// Search for triangle with area of 210
for i = 1 to count
   if triangleInfo( i, 4 ) == 210
      print triangleInfo( i, 0 ), triangleInfo( i, 1 ), triangleInfo( i, 2 ), triangleInfo( i, 3 ), triangleInfo( i, 4 )
   end if
next
 