Catalan numbers: Difference between revisions

Content added Content deleted
(Kotlin: init removed)
(Kotlin version enhanced)
Line 1,900: Line 1,900:
fun catI(n: Int): Double {
fun catI(n: Int): Double {
if (n !in catsI)
if (n !in catsI)
catsI[n] = fact(2 * n) / (fact(n + 1) * fact(n))
catsI[n] = Math.round(fact(2 * n) / (fact(n + 1) * fact(n))).toDouble()


return Math.round(catsI[n]).toDouble()
return catsI[n]
}
}


Line 1,912: Line 1,912:
for (i in 0..n - 1)
for (i in 0..n - 1)
sum += catR1(i) * catR1(n - 1 - i)
sum += catR1(i) * catR1(n - 1 - i)
sum = Math.round(sum).toDouble()


catsR1[n] = sum
catsR1[n] = sum
return Math.round(sum).toDouble()
return sum
}
}


fun catR2(n: Int): Double {
fun catR2(n: Int): Double {
if (n !in catsR2)
if (n !in catsR2)
catsR2[n] = 2.0 * (2 * (n - 1) + 1) / (n + 1) * catR2(n - 1)
catsR2[n] = Math.round(2.0 * (2 * (n - 1) + 1) / (n + 1) * catR2(n - 1)).toDouble()


return Math.round(catsR2[n]).toDouble()
return catsR2[n]
}
}