object PascalsTriangle { fun computeTriangle(rows: Int): List> { require(rows >= 0, { "Rows can't be negative!" }) if (rows == 0) return emptyList() return (1..rows).map { buildTriangleRow(it) } } private fun buildTriangleRow(row: Int): List { var m = 1 return listOf(1) + (1..row - 1).map { col -> m = m * (row - col) / col return@map m } } }