Averages/Pythagorean means: Difference between revisions
→{{header|Kotlin}}: kotlin/js compatability, harmonic mean: reduce to fold since first element is not divided
Drkameleon (talk | contribs) (added Arturo) |
(→{{header|Kotlin}}: kotlin/js compatability, harmonic mean: reduce to fold since first element is not divided) |
||
Line 1,708:
=={{header|Kotlin}}==
<lang kotlin>import kotlin.math.round
<lang scala>fun Collection<Double>.geometricMean() =▼
import kotlin.math.pow
if (isEmpty()) Double.NaN▼
else Math.pow(reduce { n1, n2 -> n1 * n2 }, 1.0 / size)▼
▲ if (isEmpty()) Double.NaN
fun Collection<Double>.harmonicMean() =
if (isEmpty() || contains(0.0)) Double.NaN
else size /
fun
round(this * 10.0.pow(len)) / 10.0.pow(len)
fun main() {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
val a = list.average() // arithmetic mean
val g = list.geometricMean()
val h = list.harmonicMean()
println("A =
println("A >= G is
require(g in h..a)
}</lang>
|