Heronian triangles: Difference between revisions

m
(Updated to compile with Nim 1.4. Improved formatting. Replaced module "strfmt" by module "strformat".)
m (→‎{{header|Wren}}: Minor tidy)
 
(27 intermediate revisions by 13 users not shown)
Line 39:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F gcd(=u, =v)
L v != 0
(u, v) = (v, u % v)
Line 70:
print(h[0.<10].map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))
print("\nAll with area 210 subject to the previous ordering:")
print(h.filter3((x, y, z) -> hero(x, y, z) == 210).map3((x, y, z) -> ‘ #14 perim: #3 area: #.’.format(String((x, y, z)), x + y + z, hero(x, y, z))).join("\n"))</langsyntaxhighlight>
 
{{out}}
Line 98:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Finalization;
with Ada.Text_IO; use Ada.Text_IO;
Line 228:
end loop;
end;
end Heronian;</langsyntaxhighlight>
{{out}}
<pre> 517 heronian triangles found :
Line 257:
=={{header|ALGOL 68}}==
{{Trans|Lua}}
<langsyntaxhighlight lang="algol68"># mode to hold details of a Heronian triangle #
MODE HERONIAN = STRUCT( INT a, b, c, area, perimeter );
# returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one #
Line 339:
OD
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 369:
=={{header|ALGOL W}}==
{{Trans|Lua}}
<langsyntaxhighlight lang="algolw">begin
% record to hold details of a Heronian triangle %
record Heronian ( integer a, b, c, area, perimeter );
Line 465:
end
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 498:
 
{{Trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">use framework "Foundation"
 
-- HERONIAN TRIANGLES --------------------------------------------------------
Line 774:
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toTitle</langsyntaxhighlight>
{{Out}}
<pre>Number of triangles found (with sides <= 200): 517
Line 801:
(7, 65, 68) 140 210
(3, 148, 149) 300 210</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">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</syntaxhighlight>
 
{{out}}
 
<pre>Number of Heronian triangles: 517
 
Ordered list of first ten Heronian triangles:
============================================================
A B C Perimeter Area
------------------------------------------------------------
3 4 5 12 6
5 5 6 16 12
5 5 8 18 12
4 13 15 32 24
5 12 13 30 30
9 10 17 36 36
3 25 26 54 36
7 15 20 42 42
10 13 13 36 60
8 15 17 40 60
 
Ordered list of Heronian triangles with area 210:
============================================================
A B C Perimeter Area
------------------------------------------------------------
17 25 28 70 210
20 21 29 70 210
12 35 37 84 210
17 28 39 84 210
7 65 68 140 210
3 148 149 300 210</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Primitive_Heronian_triangles(MaxSide){
obj :=[]
loop, % MaxSide {
Line 821 ⟶ 904:
x := StrSplit(x, "`t"), y := StrSplit(y, "`t")
return x.1 > y.1 ? 1 : x.1 < y.1 ? -1 : x.2 > y.2 ? 1 : x.2 < y.2 ? -1 : 0
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">res := Primitive_Heronian_triangles(200)
loop, parse, res, `n, `r
{
Line 837 ⟶ 920:
. "`nResults for Area = 210:"
. "`n" "Area`tPerimeter`tSides`n" res3
return</langsyntaxhighlight>
Outputs:<pre>517 results found
 
Line 867 ⟶ 950:
----
'''IMPORTANT''': This is a C99 compatible implementation. May result in errors on earlier compilers.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 988 ⟶ 1,071:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output :
<pre>
Line 1,018 ⟶ 1,101:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight Csharplang="csharp">using System;
using System.Collections.Generic;
 
Line 1,075 ⟶ 1,158:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 1,102 ⟶ 1,185:
 
=={{header|C++}}==
{{Works with|C++1117}}
<langsyntaxhighlight lang="cpp">#include <algorithmtuple>
#include <cmathvector>
#include <numeric>
#include <iostream>
#include <tuplealgorithm>
#include <vector>
 
#include <cmath>
int gcd(int a, int b)
 
{
struct Triangle {
int rem = 1, dividend, divisor;
int a{};
std::tie(divisor, dividend) = std::minmax(a, b);
while (rem != 0)int b{};
int c{};
rem = dividend % divisor;
 
if (rem != 0) {
[[nodiscard]] constexpr auto perimeter() const noexcept { return a + b + c; }
dividend = divisor;
 
divisor = rem;
[[nodiscard]] constexpr auto area() const noexcept {
}
const auto p_2 = static_cast<double>(perimeter()) / 2;
const auto area_sq = p_2 * (p_2 - a) * (p_2 - b) * (p_2 - c);
return std::sqrt(area_sq);
}
return divisor;
}
 
struct Triangle
{
int a;
int b;
int c;
};
 
int perimeter(const Triangle& triangle)
{
return triangle.a + triangle.b + triangle.c;
}
 
auto generate_triangles(int side_limit = 200) {
double area(const Triangle& t)
{
double p_2 = perimeter(t) / 2.;
double area_sq = p_2 * ( p_2 - t.a ) * ( p_2 - t.b ) * ( p_2 - t.c );
return sqrt(area_sq);
}
 
std::vector<Triangle> generate_triangles(int side_limit = 200)
{
std::vector<Triangle> result;
for(int a = 1; a <= side_limit; ++a)
for(int b = 1; b <= a; ++b)
for(int c = a + 1 - b; c <= b; ++c) // skip too-small values of c, which will violate triangle inequality
{
Triangle t{ a, b, c };
doubleconst auto t_area = t.area(t);
if (t_area == 0) continue;
if( (std::floor(t_area) == std::ceil(t_area) && std::gcd(a, std::gcd(b, c)) == 1)
result.push_back(t);
}
Line 1,158 ⟶ 1,224:
}
 
bool compare(const Triangle& lhs, const Triangle& rhs) noexcept {
return std::make_tuple(lhs.area(), lhs.perimeter(), std::max(lhs.a, std::max(lhs.b, lhs.c))) <
{
return std::make_tuple(rhs.area(lhs), rhs.perimeter(lhs), std::max(lhsrhs.a, std::max(lhsrhs.b, lhsrhs.c))) <;
std::make_tuple(area(rhs), perimeter(rhs), std::max(rhs.a, std::max(rhs.b, rhs.c)));
}
 
struct area_compare {
[[nodiscard]] constexpr bool operator()(const Triangle& t, int i) const noexcept { return t.area() < i; }
{
[[nodiscard]] constexpr bool operator()(int i, const Triangle& t,) intconst i)noexcept { return area(t)i < it.area(); }
bool operator()(int i, const Triangle& t) { return i < area(t); }
};
 
int main() {
{
auto tri = generate_triangles();
std::cout << "There are " << tri.size() << " primitive Heronian triangles with sides up to 200\n\n";
Line 1,179 ⟶ 1,242:
std::cout << "area\tperimeter\tsides\n";
for(int i = 0; i < 10; ++i)
std::cout << area(tri[i].area() << '\t' << perimeter(tri[i].perimeter() << "\t\t" <<
tri[i].a << 'x' << tri[i].b << 'x' << tri[i].c << '\n';
 
Line 1,186 ⟶ 1,249:
std::cout << "area\tperimeter\tsides\n";
for(auto it = range.first; it != range.second; ++it)
std::cout << area(*it).area() << '\t' << perimeter(*it).perimeter() << "\t\t" <<
it->a << 'x' << it->b << 'x' << it->c << '\n';
}</langsyntaxhighlight>
{{out}}
<pre>There are 517 primitive Heronian triangles with sides up to 200
Line 1,216 ⟶ 1,279:
=={{header|CoffeeScript}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="coffeescript">heronArea = (a, b, c) ->
s = (a + b + c) / 2
Math.sqrt s * (s - a) * (s - b) * (s - c)
Line 1,262 ⟶ 1,325:
for i in list
if i[4] == 210
console.log i[0..2].join(' x ') + ', p = ' + i[3]</langsyntaxhighlight>
{{out}}
<pre>primitive Heronian triangles with sides up to 200: 517
Line 1,287 ⟶ 1,350:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits, std.typecons;
 
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
Line 1,331 ⟶ 1,394:
"\nAll with area 210 subject to the previous ordering:".writeln;
showTriangles(h.filter!(t => t[].hero == 210));
}</langsyntaxhighlight>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 1,356 ⟶ 1,419:
210 140 7x65x68
210 300 3x148x149</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Heronian_triangles#Pascal Pascal].
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; returns quintuple (A s a b c)
;; or #f if not hero
Line 1,384 ⟶ 1,449:
(define (print-laurels H)
(writeln '🌿🌿 (length H) 'heroes '🌿🌿))
</syntaxhighlight>
</lang>
{{out}}
<pre>(define H (heroes))
Line 1,414 ⟶ 1,479:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Heronian do
def triangle?(a,b,c) when a+b <= c, do: false
def triangle?(a,b,c) do
Line 1,449 ⟶ 1,514:
|> Enum.each(fn {a, b, c} ->
IO.puts "#{a}\t#{b}\t#{c}\t#{a+b+c}\t#{area_size}"
end)</langsyntaxhighlight>
 
{{out}}
Line 1,474 ⟶ 1,539:
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM HERON
 
Line 1,542 ⟶ 1,607:
END FOR
END PROGRAM
</syntaxhighlight>
</lang>
<pre>Number of triangles: 517
3 4 5 12 6
Line 1,563 ⟶ 1,628:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors assocs backtrack combinators.extras
combinators.short-circuit formatting io kernel locals math
math.functions math.order math.parser math.ranges mirrors qw
Line 1,624 ⟶ 1,689:
[ first10 nl ] [ area210= ] bi ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,656 ⟶ 1,721:
=={{header|Fortran}}==
Earlier Fortran doesn't offer special functions such as SUM, PRODUCT and MAXVAL of arrays, nor the ability to create compound data aggregates such as STASH to store a triangle's details. Simple code would have to be used in the absence of such conveniences, and multiple ordinary arrays rather than an array of a compound data entity. Rather than attempt to create the candidate triangles in the desired order, the simple approach is to sort a list of triangles, and using an XNDX array evades tossing compound items about. Rather than create a procedure to do the sorting, a comb sort is not too much trouble to place in-line once. Further, since the ordering is based on a compound key, having only one comparison to code is a boon. The three-way-if statement is central to the expedient evaluation of a compound sort key, but this facility is deprecated by the modernists, with no alternative offered that avoids re-comparison of parts.
<syntaxhighlight lang="fortran">
<lang Fortran>
MODULE GREEK MATHEMATICIANS !Two millenia back and more.
CONTAINS
Line 1,764 ⟶ 1,829:
END DO !Just thump through the lot.
END
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,789 ⟶ 1,854:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 02-05-2016
' compile with: fbc -s console
 
Line 1,935 ⟶ 2,000:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>There are 517 Heronian triangles with sides <= 200
Line 1,967 ⟶ 2,032:
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">
text,,,,,70// Set width of tabs
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
 
Line 1,988 ⟶ 2,050:
 
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
 
Line 2,025 ⟶ 2,087:
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
Line 2,050 ⟶ 2,112:
// 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
 
</lang>
HandleEvents
</syntaxhighlight>
 
Output:
Line 2,085 ⟶ 2,149:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,154 ⟶ 2,218:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,181 ⟶ 2,245:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.List as L
import Data.Maybe
import Data.Ord
Line 2,243 ⟶ 2,307:
mapM_ printTri $ take 10 tris
putStrLn ""
mapM_ printTri $ filter ((== 210) . fifth) tris</langsyntaxhighlight>
{{out}}
<pre>Heronian triangles found: 517
Line 2,270 ⟶ 2,334:
'''Hero's formula Implementation'''
 
<langsyntaxhighlight Jlang="j">a=: 0&{"1
b=: 1&{"1
c=: 2&{"1
Line 2,276 ⟶ 2,340:
area=: 2 %: s*(s-a)*(s-b)*(s-c) NB. Hero's formula
perim=: +/"1
isPrimHero=: (0&~: * (= <.@:+))@area * 1 = a +. b +. c</langsyntaxhighlight>
 
We exclude triangles with zero area, triangles with complex area, non-integer area, and triangles whose sides share a common integer multiple.
Line 2,284 ⟶ 2,348:
The implementation above uses the symbols as given in the formula at the top of the page, making it easier to follow along as well as spot any errors. That formula distinguishes between the individual sides of the triangles but J could easily treat these sides as a single entity or array. The implementation below uses this "typical J" approach:
 
<langsyntaxhighlight Jlang="j">perim=: +/"1
s=: -:@:perim
area=: [: %: s * [: */"1 s - ] NB. Hero's formula
isNonZeroInt=: 0&~: *. (= <.@:+)
isPrimHero=: isNonZeroInt@area *. 1 = +./&.|:</langsyntaxhighlight>
 
'''Required examples'''
 
<langsyntaxhighlight Jlang="j"> Tri=:(1-i.3)+"1]3 comb 202 NB. distinct triangles with sides <= 200
HeroTri=: (#~ isPrimHero) Tri NB. all primitive Heronian triangles with sides <= 200
 
Line 2,318 ⟶ 2,382:
17 28 39 _ 84 210
7 65 68 _ 140 210
3 148 149 _ 300 210</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.ArrayList;
 
public class Heron {
Line 2,397 ⟶ 2,461:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,427 ⟶ 2,491:
===Imperative===
 
<syntaxhighlight lang="javascript">
<lang JavaScript>
window.onload = function(){
var list = [];
Line 2,478 ⟶ 2,542:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 2,515 ⟶ 2,579:
ES6 JavaScript introduces syntactic sugar for list comprehensions, but the list monad pattern can already be used in ES5 – indeed in any language which supports the use of higher-order functions.
 
<langsyntaxhighlight JavaScriptlang="javascript">(function (n) {
var chain = function (xs, f) { // Monadic bind/chain
Line 2,596 ⟶ 2,660:
) + '\n\n';
})(200);</langsyntaxhighlight>
 
{{out}}
Line 2,651 ⟶ 2,715:
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq"># input should be an array of the lengths of the sides
def hero:
(add/2) as $s
Line 2,690 ⟶ 2,754:
( .[] | select( hero == 210 ) | "\(rjust(11)) \(add|rjust(3)) \(hero|rjust(4))" ) ;
 
task(200)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ time jq -n -r -f heronian.jq
The number of primitive Heronian triangles with sides up to 200: 517
The first ten when ordered by increasing area, then perimeter, then maximum sides:
Line 2,713 ⟶ 2,777:
[17,28,39] 84 210
[7,65,68] 140 210
[3,148,149] 300 210</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 2,719 ⟶ 2,783:
 
'''Types and Functions'''
<syntaxhighlight lang="julia">
<lang Julia>
type IntegerTriangle{T<:Integer}
a::T
Line 2,746 ⟶ 2,810:
σ^2 == t
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
slim = 200
 
Line 2,783 ⟶ 2,847:
println(@sprintf "%6d %3d %3d %4d %4d" t.a t.b t.c t.σ t.p)
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,812 ⟶ 2,876:
=={{header|Kotlin}}==
{{trans|Scala}}
<langsyntaxhighlight lang="scala">import java.util.ArrayList
 
object Heron {
Line 2,875 ⟶ 2,939:
}
 
fun main(args: Array<String>) = Heron.run()</langsyntaxhighlight>
{{out}}
<pre>Number of primitive Heronian triangles with sides up to 200: 517
Line 2,900 ⟶ 2,964:
7 x 65 x 68 140 210
3 x 148 x 149 300 210</pre>
 
=={{header|Logtalk}}==
 
Implemented as a parametric object, the solution to making primitive Heronian triangles would look something like this:
 
<syntaxhighlight lang="logtalk">
% In this example we assume that A<=B<=C.
% Non-pedagogical code would verify and force this.
:- object(triangle(_A_, _B_, _C_)).
 
:- public([a/1, b/1, c/1, area/1, perimeter/1, primitive/0]).
 
a(_A_). b(_B_). c(_C_).
 
area(A) :-
AB is _A_ + _B_,
AB @> _C_, % you can't make a triangle if one side is half or longer the perimeter
s(S),
A is sqrt(S * (S - _A_) * (S - _B_) * (S - _C_)).
 
perimeter(P) :-
P is _A_ + _B_ + _C_.
 
primitive :- heronian, gcd(1).
 
% helper predicates
 
heronian :-
integer(_A_),
integer(_B_),
integer(_C_),
area(A),
A > 0.0,
0.0 is float_fractional_part(A).
 
gcd(G) :- G is gcd(_A_, gcd(_B_, _C_)).
 
s(S) :- perimeter(P), S is P / 2.
 
:- end_object.
</syntaxhighlight>
 
A quickly hacked-together test that produces the output for the task assignment would look something like this:
 
<syntaxhighlight lang="logtalk">
:- object(test_triangle).
 
:- uses(integer, [between/3]).
:- uses(list, [length/2, member/2, sort/3, take/3]).
:- uses(logtalk, [print_message(information, heronian, Message) as print(Message)]).
 
:- public(start/0).
 
start :-
 
gather_primitive_heronians(Primitives),
length(Primitives, L),
print('There are ~w primitive Heronian triangles with sides under 200.~n'+[L]),
 
sort(order_by(area), Primitives, AreaSorted),
take(10, AreaSorted, Area10),
print(@'The first ten found, ordered by area, are:\n'),
display_each_element(Area10),
 
sort(order_by(perimeter), Primitives, PerimeterSorted),
take(10, PerimeterSorted, Perimeter10),
print(@'The first ten found, ordered by perimeter, are:\n'),
display_each_element(Perimeter10),
 
findall(
t(A, B, C, 210.0, Perimeter),
member(t(A, B, C, 210.0, Perimeter), Primitives),
Area210
),
print(@'The list of those with an area of 210 is:\n'),
display_each_element(Area210).
 
% localized helper predicates
 
% display a single element in the provided format
display_single_element(t(A,B,C,Area,Perimeter)) :-
format(F),
print(F+[A, B, C, Area, Perimeter]).
 
% display each element in a list of elements, printing a header first
display_each_element(L) :-
print(@' A B C Area Perimeter'),
print(@'=== === === ======= ========='),
forall(member(T, L), display_single_element(T)),
print(@'\n').
 
format('~|~` t~w~3+~` t~w~4+~` t~w~4+~` t~w~8+~` t~w~7+').
 
% collect all the primitive heronian triangles within the boundaries of the provided task
gather_primitive_heronians(Primitives) :-
findall(
t(A, B, C, Area, Perimeter),
(
between(3, 200, A),
between(A, 200, B),
between(B, 200, C),
triangle(A, B, C)::primitive,
triangle(A, B, C)::area(Area),
triangle(A, B, C)::perimeter(Perimeter)
),
Primitives
).
 
order_by(_, =, T, T) :- !.
order_by(area, <, t(_,_,_,Area1,_), t(_,_,_,Area2,_)) :- Area1 < Area2, !.
order_by(area, >, t(_,_,_,Area1,_), t(_,_,_,Area2,_)) :- Area1 > Area2, !.
order_by(perimeter, <, t(_,_,_,_,Perimeter1), t(_,_,_,_,Perimeter2)) :- Perimeter1 < Perimeter2, !.
order_by(perimeter, >, t(_,_,_,_,Perimeter1), t(_,_,_,_,Perimeter2)) :- Perimeter1 > Perimeter2, !.
order_by(_, <, t(A1,_,_,_,_), t(A2,_,_,_,_)) :- A1 < A2, !.
order_by(_, <, t(_,B1,_,_,_), t(_,B2,_,_,_)) :- B1 < B2, !.
order_by(_, <, t(_,_,C1,_,_), t(_,_,C2,_,_)) :- C1 < C2, !.
order_by(_, >, _, _).
 
:- end_object.
</syntaxhighlight>
 
{{Out}}
 
<pre>
?- test_triangle::start.
% There are 517 primitive Heronian triangles with sides under 200.
 
% The first ten found, ordered by area, are:
 
% A B C Area Perimeter
% === === === ======= =========
% 3 4 5 6.0 12
% 5 5 6 12.0 16
% 5 5 8 12.0 18
% 4 13 15 24.0 32
% 5 12 13 30.0 30
% 3 25 26 36.0 54
% 9 10 17 36.0 36
% 7 15 20 42.0 42
% 6 25 29 60.0 60
% 8 15 17 60.0 40
%
 
% The first ten found, ordered by perimeter, are:
 
% A B C Area Perimeter
% === === === ======= =========
% 3 4 5 6.0 12
% 5 5 6 12.0 16
% 5 5 8 12.0 18
% 5 12 13 30.0 30
% 4 13 15 24.0 32
% 9 10 17 36.0 36
% 10 13 13 60.0 36
% 8 15 17 60.0 40
% 7 15 20 42.0 42
% 13 14 15 84.0 42
%
 
% The list of those with an area of 210 is:
 
% A B C Area Perimeter
% === === === ======= =========
% 3 148 149 210.0 300
% 7 65 68 210.0 140
% 12 35 37 210.0 84
% 17 25 28 210.0 70
% 17 28 39 210.0 84
% 20 21 29 210.0 70
%
 
true.
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Returns the details of the Heronian Triangle with sides a, b, c or nil if it isn't one
local function tryHt( a, b, c )
local result
Line 2,962 ⟶ 3,199:
local t = ht[ htPos ];
if t.area == 210 then htPrint( t ) end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,989 ⟶ 3,226:
3 148 149 210 300
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Heron]
Heron[a_, b_, c_] := With[{s = (a + b + c)/2}, Sqrt[s (s - a) (s - b) (s - c)]]
PrintTemporary[Dynamic[{a, b, c}]];
results = Reap[
Do[
If[a < b + c \[And] b < c + a \[And] c < a + b,
If[GCD[a, b, c] == 1,
If[IntegerQ[Heron[a, b, c]],
Sow[<|"Sides" -> {a, b, c}, "Area" -> Heron[a, b, c],
"Perimeter" -> a + b + c, "MaximumSide" -> Max[a, b, c]|>]
]
]
]
,
{a, 1, 200},
{b, a, 200},
{c, b, 200}
]
][[2, 1]];
results = SortBy[results, {#["Area"] &, #["Perimeter"] &, #["MaximumSide"] &}];
results // Length
Take[results, 10] // Dataset
Select[results, #["Area"] == 210 &] // Dataset</syntaxhighlight>
{{out}}
<pre>517
 
Sides Area Perimeter MaximumSide
{3,4,5} 6 12 5
{5,5,6} 12 16 6
{5,5,8} 12 18 8
{4,13,15} 24 32 15
{5,12,13} 30 30 13
{9,10,17} 36 36 17
{3,25,26} 36 54 26
{7,15,20} 42 42 20
{10,13,13} 60 36 13
{8,15,17} 60 40 17
 
Sides Area Perimeter MaximumSide
{17,25,28} 210 70 28
{20,21,29} 210 70 29
{12,35,37} 210 84 37
{17,28,39} 210 84 39
{7,65,68} 210 140 68
{3,148,149} 210 300 149</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import std/[math, algorithm, lenientops, strformat, sequtils]
 
type HeronianTriangle = tuple[a, b, c: int; p: int; area: int]
 
# Functions with three operands.
func `$` (t: HeronianTriangle): string =
func max(a, b, c: int): int = max(a, max(b, c))
func gcd(a, b, c: int): int = gcd(a, gcd(b, c))
 
func cmp(x, y: HeronianTriangle): int =
## Compare two Heronian triangles.
result = cmp(x.area, y.area)
if result == 0:
result = cmp(x.p, y.p)
if result == 0:
result = cmp(max(x.a, x.b, x.c), max(y.a, y.b, y.c))
 
func `$`(t: HeronianTriangle): string =
## Return the representation of a Heronian triangle.
fmt"{t.a:3d}, {t.b:3d}, {t.c:3d} {t.p:7d} {t.area:8d}"
 
 
func hero(a, b, c: int): float =
## Return the area of a triangle using Hero's formula.
let s = (a + b + c) / 2
result = sqrt(s * (s - a) * (s - b) * (s - c))
 
func isHeronianTriangle(x: float): bool = x > 0 and ceil(x) == x
 
func gcd(x, y: int): int =
var
(dividend, divisor) = if x > y: (x, y) else: (y, x)
remainder = dividend mod divisor
 
while remainder != 0:
dividend = divisor
divisor = remainder
remainder = dividend mod divisor
result = divisor
 
func max(a, b, c: int): int = max(a, max(b, c))
func gcd(a, b, c: int): int = gcd(a, gcd(b, c))
 
const Header = " Sides Perimeter Area\n------------- --------- ----"
Line 3,027 ⟶ 3,312:
for a in 1..b:
let area = hero(a, b, c)
if isHeronianTriangle(area).isHeronianTriangle and gcd(a, b, c) == 1:
let t: HeronianTriangle = (a, b, c, a + b + c, area.toInt)
list.add( t)
 
list.sort(cmp)
echo "Number of Heronian triangles: ", list.len
 
list.sort do (x, y: HeronianTriangle) -> int:
result = cmp(x.area, y.area)
if result == 0:
result = cmp(x.p, y.p)
if result == 0:
result = cmp(max(x.a, x.b, x.c), max(y.a, y.b, y.c))
 
echo "\nOrdered list of first ten Heronian triangles:"
Line 3,047 ⟶ 3,326:
echo Header
for t in list.filterIt(it.area == 210): echo t
</syntaxhighlight>
</lang>
{{out}}
<pre>Number of Heronian triangles: 517
Line 3,077 ⟶ 3,356:
=={{header|ooRexx}}==
Derived from REXX with some changes
<langsyntaxhighlight lang="rexx">/*REXX pgm generates primitive Heronian triangles by side length & area.*/
Call time 'R'
Numeric Digits 12
Line 3,214 ⟶ 3,493:
::requires rxmath library
::routine sqrt
Return rxCalcSqrt(arg(1),14)</langsyntaxhighlight>
{{out}}
<pre>517 primitive Heronian triangles found with side length up to 200 (inclusive).
Line 3,241 ⟶ 3,520:
=={{header|PARI/GP}}==
 
<langsyntaxhighlight lang="parigp">Heron(v)=my([a,b,c]=v); (a+b+c)*(-a+b+c)*(a-b+c)*(a+b-c) \\ returns 16 times the squared area
is(a,b,c)=(a+b+c)%2==0 && gcd(a,gcd(b,c))==1 && issquare(Heron([a,b,c]))
v=List(); for(a=1,200,for(b=a+1,200,for(c=b+1,200, if(is(a,b,c),listput(v, [a,b,c])))));
Line 3,251 ⟶ 3,530:
vecsort(u, (a,b)->Heron(a)-Heron(b))
vecsort(u, (a,b)->vecsum(a)-vecsum(b))
vecsort(u, 3) \\ shortcut: order by third component</langsyntaxhighlight>
{{out}}
<pre>%1 = [[1, 2, 3], [1, 3, 4], [1, 4, 5], [1, 5, 6], [1, 6, 7], [1, 7, 8], [1, 8, 9], [1, 9, 10], [1, 10, 11], [1, 11, 12]]
Line 3,262 ⟶ 3,541:
=={{header|Pascal}}==
{{Trans|Lua}}
<langsyntaxhighlight lang="pascal">program heronianTriangles ( input, output );
type
(* record to hold details of a Heronian triangle *)
Line 3,362 ⟶ 3,641:
if t^.area = 210 then htPrint( t )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,392 ⟶ 3,671:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util qw(max);
Line 3,454 ⟶ 3,733:
}
 
&main();</langsyntaxhighlight>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,479 ⟶ 3,758:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">-->
<lang Phix>function heroArea(integer a, b, c)
<span style="color: #008080;">function</span> <span style="color: #000000;">heroArea</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
atom s = (a+b+c)/2
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
return sqrt(s*(s-a)*(s-b)*(s-c))
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)*(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">-</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)*(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">-</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function hero(atom h)
<span style="color: #008080;">function</span> <span style="color: #000000;">hero</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
return remainder(h,1)=0 and h>0
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
sequence list = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
integer tries = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">tries</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for a=1 to 200 do
<span style="color: #008080;">for</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">200</span> <span style="color: #008080;">do</span>
for b=1 to a do
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">a</span> <span style="color: #008080;">do</span>
for c=1 to b do
<span style="color: #008080;">for</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">b</span> <span style="color: #008080;">do</span>
tries += 1
<span style="color: #000000;">tries</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if gcd({a,b,c})=1 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">gcd</span><span style="color: #0000FF;">({</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">})=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
atom hArea = heroArea(a,b,c)
<span style="color: #004080;">atom</span> <span style="color: #000000;">hArea</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">heroArea</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
if hero(hArea) then
<span style="color: #008080;">if</span> <span style="color: #000000;">hero</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hArea</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
list = append(list,{hArea,a+b+c,a,b,c})
<span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">hArea</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
list = sort(list)
<span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">)</span>
printf(1,"Primitive Heronian triangles with sides up to 200: %d (of %,d tested)\n\n",{length(list),tries})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Primitive Heronian triangles with sides up to 200: %d (of %,d tested)\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">),</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">})</span>
printf(1,"First 10 ordered by area/perimeter/sides:\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 10 ordered by area/perimeter/sides:\n"</span><span style="color: #0000FF;">)</span>
printf(1,"area perimeter sides")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"area perimeter sides\n"</span><span style="color: #0000FF;">)</span>
for i=1 to 10 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
printf(1,"\n%4d %3d %dx%dx%d",list[i])
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d %3d %dx%dx%d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"\n\narea = 210:\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\narea = 210:\n"</span><span style="color: #0000FF;">)</span>
printf(1,"area perimeter sides")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"area perimeter sides\n"</span><span style="color: #0000FF;">)</span>
for i=1 to length(list) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if list[i][1]=210 then
<span style="color: #008080;">if</span> <span style="color: #000000;">list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">210</span> <span style="color: #008080;">then</span>
printf(1,"\n%4d %3d %dx%dx%d",list[i])
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d %3d %dx%dx%d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,545 ⟶ 3,826:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">
function Get-Gcd($a, $b){
if($a -ge $b){
Line 3,593 ⟶ 3,874:
}
}
</syntaxhighlight>
</lang>
{{out}}
<syntaxhighlight lang="text">
Primitive Heronian triangles with sides up to 200: 517
 
Line 3,619 ⟶ 3,900:
7 65 68 140 210
3 148 149 300 210
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">from __future__ import division, print_function
from math import gcd, sqrt
 
Line 3,667 ⟶ 3,948:
% (sides, sum(sides), hero(*sides)) for sides in h
if hero(*sides) == 210))
</syntaxhighlight>
</lang>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,695 ⟶ 3,976:
Mostly adopted from Python implementation:
 
<syntaxhighlight lang="r">
<lang R>
area <- function(a, b, c) {
s = (a + b + c) / 2
Line 3,732 ⟶ 4,013:
cat("Showing the first ten ordered first by perimeter, then by area:\n")
print(head(r[order(x=r[,"perimeter"],y=r[,"area"]),],n=10))
</syntaxhighlight>
</lang>
 
{{out}}
 
<syntaxhighlight lang="text">There are 517 Heronian triangles up to a maximal side length of 200.
Showing the first ten ordered first by perimeter, then by area:
a b c perimeter area
Line 3,748 ⟶ 4,029:
[8,] 8 15 17 40 60
[9,] 7 15 20 42 42
[10,] 13 14 15 42 84</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="text">#lang at-exp racket
(require data/order scribble/html)
 
Line 3,796 ⟶ 4,077:
@; Show a similar ordered table for those triangles with area = 210
@(triangles->table (tri-sort (filter (λ(t) (eq? 210 (car t))) ts)))
}))</langsyntaxhighlight>
 
This program generates HTML, so the output is inline with the page, not in a <code>&lt;pre></code> block.
Line 3,829 ⟶ 4,110:
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
<syntaxhighlight lang="raku" perl6line>sub hero($a, $b, $c) {
my $s = ($a + $b + $c) / 2;
($s * ($s - $a) * ($s - $b) * ($s - $c)).sqrt;
Line 3,871 ⟶ 4,152:
say "\nArea $witharea:";
show @h.grep: *[0] == $witharea;
}</langsyntaxhighlight>
{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517
Line 3,927 ⟶ 4,208:
This REXX version doesn't need to explicitly sort the triangles as they are listed in the proper order.
<langsyntaxhighlight lang="rexx">/*REXX program generates & displays primitive Heronian triangles by side length and area*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Line 3,979 ⟶ 4,260:
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/; return /* [↑] show any found triangles. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 4,014 ⟶ 4,295:
 
It is about eight times faster than the 1<sup>st</sup> REXX version.
<langsyntaxhighlight lang="rexx">/*REXX program generates & displays primitive Heronian triangles by side length and area*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
Line 4,059 ⟶ 4,340:
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/; return /* [↑] show any found triangles. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Heronian triangles
 
Line 4,104 ⟶ 4,385:
end
return gcd
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,129 ⟶ 4,410:
{17, 28, 39} 84 210
{20, 21, 29} 70 210
</pre>
 
=={{header|RPL}}==
We use here the <code>→V3</code> and<code>SORT</code> instructions, available for HP-48G or newer models only. <code>GCD </code> is not a built-in instruction, but it is a question of a few words:
≪ WHILE DUP REPEAT SWAP OVER MOD END DROP ABS ≫ ''''GCD'''' STO
{{trans|FreeBASIC}}
{{works with|Halcyon Calc|4.2.8}}
{| class="wikitable"
! RPL code
! Comment
|-
|
3 DUPN + + 2 / → a b c s
≪ s DUP a - * s b - * s c - *
≫ ≫ ‘'''SURF2'''’ STO
IF '''SURF2''' DUP 0 > THEN √ FP NOT ELSE DROP 0 END
≫ ‘'''HERO?'''’ STO
≪ → n
≪ { } 1 n FOR x
x n FOR y
y x 2 MOD + x y + 1 - n MIN FOR z
IF x y z '''GCD GCD''' THEN
IF x y z '''HERO?''' THEN x y z →V3 +
END END
2 STEP NEXT NEXT
≫ ≫ ‘'''TASK2'''’ RCL
|
'''SURF2''' ''( a b c → A² ) ''
s = (a+b+c)/2
A² = s(s-a)(s-b)(s-c)
return A²
'''HERO?''' ''( a b c → boolean ) ''
return true if A > 0 and √A is an integer
'''TASK2''' ''( n → { [Heronians] ) ''
for x = 1 to n
for y = x to n
for z = y+u to min(x+y-1,n) // u ensures x+y+z is even
if gcd(x,y,z) == 1
if x y z is Heronian then append to list
z += 2 to keep x+y+z even
|}
The rest of the code, which is devoted to printing the requested tables, is boring and the result is awful: native RPL works on machines with a 22-character screen.
{{in}}
<pre>
≪ → a b c
≪ c →STR WHILE DUP SIZE 4 < REPEAT " " SWAP + END
a b c + + →STR SWAP + WHILE DUP SIZE 8 < REPEAT " " SWAP + END
a b c SURF √ →STR SWAP + WHILE DUP SIZE 12 < REPEAT " " SWAP + END
" (" + a →STR + " " + b →STR + " " + c →STR + ")" +
≫ ≫ 'PRTRI' STO
200 TASK2 'H' STO
≪ { } 1 H SIZE FOR j H j GET ARRY→ DROP PRTRI + NEXT SORT 'H2' STO ≫ EVAL
"Area P. LS (triangle)"
1 10 H2 SUB
≪ { } 1 H2 SIZE FOR j H2 j GET IF DUP 1 4 SUB " 210" == THEN + END NEXT ≫ EVAL
</pre>
{{out}}
<pre style="height:40ex;overflow:scroll;">
4: 517
3: "Area P. LS (triangle)"
2: { " 6 12 5 (3 4 5)"
" 12 16 6 (5 5 6)"
" 12 18 8 (5 5 8)"
" 24 32 15 (4 13 15)"
" 30 30 13 (5 12 13)"
" 36 36 17 (9 10 17)"
" 36 54 26 (3 25 26)"
" 42 42 20 (7 15 20)"
" 60 36 13 (10 13 13)"
" 60 40 17 (8 15 17)" }
1: { " 210 70 28 (17 25 28)"
" 210 70 29 (20 21 29)"
" 210 84 37 (12 35 37)"
" 210 84 39 (17 28 39)"
" 210 140 68 (7 65 68)"
" 210 300 149 (3 148 149)" }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Triangle
def self.valid?(a,b,c) # class method
short, middle, long = [a, b, c].sort
Line 4,177 ⟶ 4,543:
puts sorted.first(10).map(&:to_s)
puts "\nTriangles with an area of: #{area}"
sorted.each{|tr| puts tr if tr.area == area}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,201 ⟶ 4,567:
7x65x68 140 210.0
3x148x149 300 210.0
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use num_integer::Integer;
use std::{f64, usize};
 
const MAXSIZE: usize = 200;
 
#[derive(Debug)]
struct HerionanTriangle {
a: usize,
b: usize,
c: usize,
area: usize,
perimeter: usize,
}
 
fn get_area(a: &usize, b: &usize, c: &usize) -> f64 {
let s = (a + b + c) as f64 / 2.;
(s * (s - *a as f64) * (s - *b as f64) * (s - *c as f64)).sqrt()
}
 
fn is_heronian(a: &usize, b: &usize, c: &usize) -> bool {
let area = get_area(a, b, c);
// Heronian if the area is an integer number
area != 0. && area.fract() == 0.
}
 
fn main() {
let mut heronians: Vec<HerionanTriangle> = vec![];
 
(1..=MAXSIZE).into_iter().for_each(|a| {
(a..=MAXSIZE).into_iter().for_each(|b| {
(b..=MAXSIZE).into_iter().for_each(|c| {
if a + b > c && a.gcd(&b).gcd(&c) == 1 && is_heronian(&a, &b, &c) {
heronians.push(HerionanTriangle {
a,
b,
c,
perimeter: a + b + c,
area: get_area(&a, &b, &c) as usize,
})
}
})
})
});
 
// sort by area then by perimeter, then by maximum side
heronians.sort_unstable_by(|x, y| {
x.area
.cmp(&y.area)
.then(x.perimeter.cmp(&y.perimeter))
.then((x.a.max(x.b).max(x.c)).cmp(&y.a.max(y.b).max(y.c)))
});
 
println!(
"Primitive Heronian triangles with sides up to 200: {}",
heronians.len()
);
 
println!("\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:");
heronians.iter().take(10).for_each(|h| println!("{:?}", h));
 
println!("\nAll with area 210 subject to the previous ordering:");
heronians
.iter()
.filter(|h| h.area == 210)
.for_each(|h| println!("{:?}", h));
}
 
</syntaxhighlight>
{{out}}
<pre>
Primitive Heronian triangles with sides up to 200: 517
 
First ten when ordered by increasing area, then perimeter,then maximum sides:
HerionanTriangle { a: 3, b: 4, c: 5, area: 6, perimeter: 12 }
HerionanTriangle { a: 5, b: 5, c: 6, area: 12, perimeter: 16 }
HerionanTriangle { a: 5, b: 5, c: 8, area: 12, perimeter: 18 }
HerionanTriangle { a: 4, b: 13, c: 15, area: 24, perimeter: 32 }
HerionanTriangle { a: 5, b: 12, c: 13, area: 30, perimeter: 30 }
HerionanTriangle { a: 9, b: 10, c: 17, area: 36, perimeter: 36 }
HerionanTriangle { a: 3, b: 25, c: 26, area: 36, perimeter: 54 }
HerionanTriangle { a: 7, b: 15, c: 20, area: 42, perimeter: 42 }
HerionanTriangle { a: 10, b: 13, c: 13, area: 60, perimeter: 36 }
HerionanTriangle { a: 8, b: 15, c: 17, area: 60, perimeter: 40 }
 
All with area 210 subject to the previous ordering:
HerionanTriangle { a: 17, b: 25, c: 28, area: 210, perimeter: 70 }
HerionanTriangle { a: 20, b: 21, c: 29, area: 210, perimeter: 70 }
HerionanTriangle { a: 12, b: 35, c: 37, area: 210, perimeter: 84 }
HerionanTriangle { a: 17, b: 28, c: 39, area: 210, perimeter: 84 }
HerionanTriangle { a: 7, b: 65, c: 68, area: 210, perimeter: 140 }
HerionanTriangle { a: 3, b: 148, c: 149, area: 210, perimeter: 300 }
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object Heron extends scala.collection.mutable.MutableList[Seq[Int]] with App {
private final val n = 200
for (c <- 1 to n; b <- 1 to c; a <- 1 to b if gcd(gcd(a, b), c) == 1) {
Line 4,238 ⟶ 4,699:
private final val header = "\nSides Perimeter Area"
private def format: Seq[Int] => String = "\n%3d x %3d x %3d %5d %10d".format
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">class Triangle(a, b, c) {
 
has (sides, perimeter, area)
Line 4,289 ⟶ 4,750:
say sorted.first(10).join("\n")
say "\nTriangles with an area of: #{area}"
sorted.each{|tr| say tr if (tr.area == area)}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,317 ⟶ 4,778:
=={{header|Smalltalk}}==
Works with Squeak 5.x
<langsyntaxhighlight Smalltalklang="smalltalk">perimeter := [:triangle | triangle reduce: #+].
 
squaredArea := [:triangle |
Line 4,357 ⟶ 4,818:
area210 do: tabulate.
 
Transcript flush.</langsyntaxhighlight>
{{out}}
<pre>
Line 4,386 ⟶ 4,847:
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">h,t = getem(200)
#.sort(h,4,5,1,2,3)
#.output("There are ",t," Heronian triangles")
Line 4,418 ⟶ 4,879:
s = (a+b+c)/2
<= (s*(s-a)*(s-b)*(s-c))^0.5, s*2
.</langsyntaxhighlight>
{{out}}
<pre>
Line 4,445 ⟶ 4,906:
=={{header|Swift}}==
Works with Swift 1.2
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
typealias PrimitiveHeronianTriangle = (s1:Int, s2:Int, s3:Int, p:Int, a:Int)
Line 4,498 ⟶ 4,959:
for t in triangles[0...9] {
println("\(t.s1)\t\(t.s2)\t\(t.s3)\t\t\(t.p)\t\t\(t.a)")
}</langsyntaxhighlight>
 
{{out}}
Line 4,521 ⟶ 4,982:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">
if {[info commands let] eq ""} {
 
Line 4,613 ⟶ 5,074:
sqlite3 db :memory:
main db
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,641 ⟶ 5,102:
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Function heroArea(a As Integer, b As Integer, c As Integer) As Double
s = (a + b + c) / 2
On Error GoTo Err
Line 4,692 ⟶ 5,153:
End If
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre>Primitive Heronian triangles with sides up to 200: 517 (of 1353400 tested)
 
Line 4,721 ⟶ 5,182:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./sort" for Sort
import "./fmt" for Fmt
 
var isInteger = Fn.new { |n| n is Num && n.isInteger }
Line 4,770 ⟶ 5,231:
var sides = Fmt.swrite("$2d x $3d x $3d", t[0][0], t[0][1], t[0][2])
Fmt.print("$-14s $3d $3d $3d", sides, t[1], t[2], t[3])
}</langsyntaxhighlight>
 
{{out}}
Line 4,797 ⟶ 5,258:
7 x 65 x 68 210 140 68
3 x 148 x 149 210 300 149
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \for Min, GCD, StrSort, StrNCmp, and Print
 
func Hero(A, B, C); \Return area squared of triangle with sides A, B, C
int A, B, C, S;
[S:= (A+B+C)/2;
if rem(0) = 1 then return 0; \return 0 if area is not an integer
return S*(S-A)*(S-B)*(S-C);
];
 
func Heronian(A, B, C); \Return area of triangle if sides and area are integers
int A, B, C, Area2, Area;
[Area2:= Hero(A, B, C);
Area:= sqrt(Area2);
return if Area*Area = Area2 then Area else 0;
];
 
def MaxSide = 200;
int A, B, C, Area, Count, I, J, K;
char Array(1000, 5*5);
[Format(5, 0);
Count:= 0;
for A:= 1 to MaxSide do
for B:= A to MaxSide do
for C:= B to Min(A+B-1, MaxSide) do
if GCD(GCD(B,C), A) = 1 then
[Area:= Heronian(A, B, C);
if Area > 0 then
[OpenO(8);
RlOut(8, float(Area));
RlOut(8, float(A+B+C));
RlOut(8, float(C));
RlOut(8, float(B));
RlOut(8, float(A));
OpenI(8);
for I:= 0 to 25-1 do Array(Count,I):= ChIn(8);
Count:= Count+1;
];
];
Print("Count = %d\n", Count);
StrSort(Array, Count);
Print(" A B C Perim Area\n");
for I:= 0 to 10-1 do
[for J:= 4 downto 0 do
Print("%5.5s", @Array(I, J*5+K));
Print("\n");
];
Print("\n");
for I:= 0 to Count-1 do
if StrNCmp(" 210", @Array(I,0), 5) = 0 then
[for J:= 4 downto 0 do
Print("%5.5s", @Array(I, J*5+K));
Print("\n");
];
]</syntaxhighlight>
{{out}}
<pre>
Count = 517
A B C Perim Area
3 4 5 12 6
5 5 6 16 12
5 5 8 18 12
4 13 15 32 24
5 12 13 30 30
9 10 17 36 36
3 25 26 54 36
7 15 20 42 42
10 13 13 36 60
8 15 17 40 60
 
17 25 28 70 210
20 21 29 70 210
12 35 37 84 210
17 28 39 84 210
7 65 68 140 210
3 148 149 300 210
</pre>
 
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight 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
Line 4,808 ⟶ 5,347:
A:=hero(a,b,c);
(A>0) and A.modf()[1].closeTo(0.0,1.0e-6) and A //--> area or False
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">const MAX_SIDE=200;
heros:=Sink(List);
foreach a,b,c in ([1..MAX_SIDE],[a..MAX_SIDE],[b..MAX_SIDE]){
Line 4,830 ⟶ 5,369:
println("Area Perimeter Sides");
heros.filter(fcn([(h,_)]){ h==210 })
.pump(fcn(phabc){ "%3s %8d %3dx%dx%d".fmt(phabc.xplode()).println() });</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits