Heronian triangles: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: restored a missing right bracket.
scala sorting/filtering
Line 1,613:
 
=={{header|Scala}}==
Translation of Java version (same output).
<lang scala>object Heron extends scala.collection.mutable.MutableList[Seq[Int]] with App {
private final val n = 200;
for (c <- 1 to 200n; b <- 1 to c; a <- 1 to b if (gcd(gcd(a, b), c) == 1)) {
val area = heronArea(a, b, c)
if (isHeron(area))
Line 1,621:
}
 
private final val list = sortsortBy(toArrayi => (i(4), i(3))).toArray
print(s"Number of primitive Heronian triangles with sides up to 200$n: " + list.length)
print("\n\nFirst ten when ordered by increasing area, then perimeter:" + header)
for (i <- 0 to 10)
printf(format, list(i)(0), list(i)(1), list(i)(2), list(i)(3), list(i)(4))
 
print("\n\nArea = 210" + header)
forlist filter { _(i4) <-== 0210 until} list.length;foreach i_4{ i => listprintf(format, i(0), i(1), i(2), i(3), i(4)) }
if (i_4 == 210)
printf(format, list(i)(0), list(i)(1), list(i)(2), list(i)(3), i_4)
 
private def gcd(a: Int, b: Int) = {
Line 1,652 ⟶ 1,649:
 
private def isHeron(h: Double) = h % 1 == 0 && h > 0
 
private def sort(list: Array[Seq[Int]]) = {
var swapped = true
while (swapped) {
swapped = false
for (i <- 1 until list.length; i_4 = list(i)(4); i_1_4 = list(i - 1)(4))
if (i_4 < i_1_4 || i_4 == i_1_4 && list(i)(3) < list(i - 1)(3)) {
val temp = list(i)
list(i) = list(i - 1)
list(i - 1) = temp
swapped = true
}
}
list
}
 
private final val header = "\nSides Perimeter Area";