Heronian triangles: Difference between revisions
Content added Content deleted
(Added Wren) |
(Updated to compile with Nim 1.4. Improved formatting. Replaced module "strfmt" by module "strformat".) |
||
Line 2,991: | Line 2,991: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<lang nim>import math, algorithm, |
<lang nim>import math, algorithm, lenientops, strformat, sequtils |
||
⚫ | |||
type |
|||
⚫ | |||
a: int |
|||
b: int |
|||
c: int |
|||
s: float |
|||
A: int |
|||
func `$` (t: HeronianTriangle): string = |
|||
fmt |
fmt"{t.a:3d}, {t.b:3d}, {t.c:3d} {t.p:7d} {t.area:8d}" |
||
proc hero(a:int, b:int, c:int): tuple[s, A: float] = |
|||
⚫ | |||
⚫ | |||
⚫ | |||
func hero(a, b, c: int): float = |
|||
⚫ | |||
⚫ | |||
⚫ | |||
func gcd(x, y: int): int = |
|||
var |
var |
||
(dividend, divisor) = if x > y: (x, y) else: (y, x) |
(dividend, divisor) = if x > y: (x, y) else: (y, x) |
||
remainder = dividend mod divisor |
remainder = dividend mod divisor |
||
while remainder != 0: |
while remainder != 0: |
||
dividend = divisor |
dividend = divisor |
||
Line 3,020: | Line 3,014: | ||
remainder = dividend mod divisor |
remainder = dividend mod divisor |
||
result = 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------------- --------- ----" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
for b in 1..c: |
for b in 1..c: |
||
for a in 1..b: |
for a in 1..b: |
||
let |
let area = hero(a, b, c) |
||
if isHeronianTriangle( |
if isHeronianTriangle(area) and gcd(a, b, c) == 1: |
||
let t:HeronianTriangle = (a, b, c, |
let t: HeronianTriangle = (a, b, c, a + b + c, area.toInt) |
||
list.add(t) |
list.add(t) |
||
echo " |
echo "Number of Heronian triangles: ", list.len |
||
list.sort do (x, y: HeronianTriangle) -> int: |
list.sort do (x, y: HeronianTriangle) -> int: |
||
result = cmp(x. |
result = cmp(x.area, y.area) |
||
if result == 0: |
if result == 0: |
||
result = cmp(x. |
result = cmp(x.p, y.p) |
||
if result == 0: |
if result == 0: |
||
result = cmp(max(x.a, x.b, x.c), max(y.a, y.b, y.c)) |
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 .. |
for t in list[0 ..< 10]: echo t |
||
echo t |
|||
echo " |
echo "\nOrdered list of Heronian triangles with area 210:" |
||
echo Header |
|||
echo "Sides Perimeter Area" |
|||
for t in list. |
for t in list.filterIt(it.area == 210): echo t |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre>Number of Heronian triangles: 517 |
||
Ten first Heronian triangle ordered : |
|||
Ordered list of first ten Heronian triangles: |
|||
⚫ | |||
Sides Perimeter Area |
|||
------------- --------- ---- |
|||
5, 5, 6 8.000 12 |
|||
3, 4, 5 12 6 |
|||
5, 5, 6 16 12 |
|||
5, |
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 |
|||
Heronian triangle ordered with Area 210 : |
|||
8, 15, 17 40 60 |
|||
17, 25, 28 35.000 210 |
|||
Ordered list of Heronian triangles with area 210: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
------------- --------- ---- |
|||
⚫ | |||
17, 25, 28 70 210 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
7, 65, 68 140 210 |
|||
⚫ | |||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |