Anonymous user
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,
▲ HeronianTriangle = tuple
fmt
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▼
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------------- --------- ----"
for b in 1..c:
for a in 1..b:
let
if isHeronianTriangle(
let t: HeronianTriangle = (a, b, c,
list.add(t)
echo "
list.sort do (x, y: HeronianTriangle) -> int:
result = cmp(x.
if result == 0:
result = cmp(x.
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 Header
for t in list[0 ..
echo "
echo Header
for t in list.
{{out}}
<pre>
Ordered list of first ten Heronian triangles:
Sides Perimeter Area▼
------------- --------- ----
5,
10, 13, 13 36 60
Ordered list of Heronian triangles with area 210:
20, 21, 29 35.000 210▼
12, 35, 37 42.000 210▼
------------- --------- ----
17, 28, 39 42.000 210▼
17,
3, 148, 149 150.000 210</pre>▼
7, 65, 68 140 210
=={{header|ooRexx}}==
|