Jump to content

Heronian triangles: Difference between revisions

Updated to compile with Nim 1.4. Improved formatting. Replaced module "strfmt" by module "strformat".
(Added Wren)
(Updated to compile with Nim 1.4. Improved formatting. Replaced module "strfmt" by module "strformat".)
Line 2,991:
 
=={{header|Nim}}==
<lang nim>import math, algorithm, strfmtlenientops, strformat, sequtils
 
type HeronianTriangle = tuple[a, b, c: int; p: int; area: int]
type
HeronianTriangle = tuple
a: int
b: int
c: int
s: float
A: int
 
procfunc `$` (t: HeronianTriangle): string =
fmt("{t.a:3d}, {t.b:3d}, {:3d}\t{:7.3f}\t{c:3d}", {t.a,p:7d} {t.b, t.c, t.s, t.A)area:8d}"
proc hero(a:int, b:int, c:int): tuple[s, A: float] =
let s: float = (a + b + c) / 2
result = (s, sqrt( s * (s - float(a)) * (s - float(b)) * (s - float(c)) ))
proc isHeronianTriangle(x: float): bool = ceil(x) == x and x.toInt > 0
 
procfunc gcdhero(x:a, intb, yc: int): intfloat =
let s: float = (a + b + c) / 2
result = (s, sqrt( s * (s - float(a)) * (s - float(b)) * (s - float(c)) ))
 
procfunc isHeronianTriangle(x: float): bool = ceil(x) ==> x0 and ceil(x.toInt) >== 0x
 
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
Line 3,020 ⟶ 3,014:
remainder = dividend mod divisor
result = divisor
var list = newSeq[HeronianTriangle]()
const max = 200
 
func max(a, b, c: int): int = max(a, max(b, c))
for c in 1..max:
func gcd(a, b, c: int): int = gcd(a, gcd(b, c))
 
const Header = " Sides Perimeter Area\n------------- --------- ----"
 
var list: = newSeqseq[HeronianTriangle]()
const maxMax = 200
 
for c in 1..maxMax:
for b in 1..c:
for a in 1..b:
let (s, A)area = hero(a, b, c)
if isHeronianTriangle(Aarea) and gcd(a, gcd(b, c)) == 1:
let t: HeronianTriangle = (a, b, c, sa + b + c, Aarea.toInt)
list.add(t)
 
echo "NumbersNumber of Heronian Triangle triangles: ", list.len
 
list.sort do (x, y: HeronianTriangle) -> int:
result = cmp(x.Aarea, y.Aarea)
if result == 0:
result = cmp(x.sp, y.sp)
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:"
echo "Ten first Heronian triangle ordered : "
echo Header
echo "Sides Perimeter Area"
for t in list[0 .. < 10]: echo t
echo t
 
echo "Heronian\nOrdered trianglelist orderedof Heronian triangles with Areaarea 210 : "
echo Header
echo "Sides Perimeter Area"
for t in list.filterfilterIt(proc (x: HeronianTriangle): bool = xit.Aarea == 210): echo t
echo t</lang>
{{out}}
<pre>NumbersNumber of Heronian Triangle triangles: 517
 
Ten first Heronian triangle ordered :
Ordered list of first ten Heronian triangles:
Sides Perimeter Area
3, Sides 4, 5 6.000 Perimeter 6Area
------------- --------- ----
5, 5, 6 8.000 12
53, 54, 8 5 9.000 12 6
45, 13 5, 15 6 16.000 24 12
5, 12 5, 13 15.000 8 30 18 12
94, 1013, 17 15 18.000 36 32 24
35, 2512, 26 13 27.000 36 30 30
79, 1510, 20 17 21.000 42 36 36
10 3, 1325, 13 26 54 18.000 6036
87, 15, 17 20.000 60 42 42
10, 13, 13 36 60
Heronian triangle ordered with Area 210 :
Sides 8, 15, 17 Perimeter Area 40 60
 
17, 25, 28 35.000 210
Ordered list of Heronian triangles with area 210:
20, 21, 29 35.000 210
Sides Sides Perimeter Area
12, 35, 37 42.000 210
------------- --------- ----
17, 28, 39 42.000 210
17, 725, 65,28 68 70.000 210
20, 21, 29 35.000 70 210
3, 148, 149 150.000 210</pre>
12, 35, 37 42.000 84 210
17, 28, 39 42.000 84 210
7, 65, 68 140 210
3, 148, 149 150.000 300 210</pre>
 
=={{header|ooRexx}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.