Heronian triangles: Difference between revisions

Added Wren
(→‎{{header|Smalltalk}}: oops! sort by area first, not by perimeter first! also use a,b,c rather than i,j,k)
(Added Wren)
Line 4,715:
210 140 68x65x7
210 300 149x148x3</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int, Nums
import "/sort" for Sort
import "/fmt" for Fmt
 
var isInteger = Fn.new { |n| n is Num && n.isInteger }
 
var primHeronian = Fn.new { |a, b, c|
if (!(isInteger.call(a) && isInteger.call(b) && isInteger.call(c))) return [false, 0, 0]
if (Int.gcd(Int.gcd(a, b), c) != 1) return [false, 0, 0]
var p = a + b + c
var s = p / 2
var A = (s * (s - a) * (s - b) * (s - c)).sqrt
if (A > 0 && isInteger.call(A)) return [true, A, p]
return [false, 0, 0]
}
 
var ph = []
for (a in 1..200) {
for (b in a..200) {
for (c in b..200) {
var res = primHeronian.call(a, b, c)
if (res[0]) {
var sides = [a, b, c]
ph.add([sides, res[1], res[2], Nums.max(sides)])
}
}
}
}
System.print("There are %(ph.count) primitive Heronian trangles with sides <= 200.")
 
var cmp = Fn.new { |e1, e2|
if (e1[1] != e2[1]) return (e1[1] - e2[1]).sign
if (e1[2] != e2[2]) return (e1[2] - e2[2]).sign
return (e1[3] - e2[3]).sign
}
Sort.quick(ph, 0, ph.count-1, cmp)
System.print("\nThe first 10 such triangles in sorted order are:")
System.print(" Sides Area Perimeter Max Side")
for (t in ph.take(10)) {
var sides = Fmt.swrite("$2d x $2d x $2d", t[0][0], t[0][1], t[0][2])
Fmt.print("$-14s $2d $2d $2d", sides, t[1], t[2], t[3])
}
 
System.print("\nThe triangles in the previously sorted order with an area of 210 are:")
System.print(" Sides Area Perimeter Max Side")
for (t in ph.where { |e| e[1] == 210 }) {
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])
}</lang>
 
{{out}}
<pre>
There are 517 primitive Heronian trangles with sides <= 200.
 
The first 10 such triangles in sorted order are:
Sides Area Perimeter Max Side
3 x 4 x 5 6 12 5
5 x 5 x 6 12 16 6
5 x 5 x 8 12 18 8
4 x 13 x 15 24 32 15
5 x 12 x 13 30 30 13
9 x 10 x 17 36 36 17
3 x 25 x 26 36 54 26
7 x 15 x 20 42 42 20
10 x 13 x 13 60 36 13
8 x 15 x 17 60 40 17
 
The triangles in the previously sorted order with an area of 210 are:
Sides Area Perimeter Max Side
17 x 25 x 28 210 70 28
20 x 21 x 29 210 70 29
12 x 35 x 37 210 84 37
17 x 28 x 39 210 84 39
7 x 65 x 68 210 140 68
3 x 148 x 149 210 300 149
</pre>
 
=={{header|zkl}}==
9,488

edits