From 3b643db4198943e2c4e998a93f878af40d41bba8 Mon Sep 17 00:00:00 2001 From: Anthony C Date: Tue, 1 Aug 2017 15:02:20 -0400 Subject: [PATCH] Kotlin - Completed Pascal's Triangle --- .../src/main/kotlin/PascalsTriangle.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt diff --git a/kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt b/kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt new file mode 100644 index 0000000..194efa9 --- /dev/null +++ b/kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt @@ -0,0 +1,27 @@ +class PascalsTriangle { + companion object { + fun computeTriangle(row: Int): List>{ + require(row >= 0){} + val returnList = mutableListOf>() + for (i in 0..row-1){ + val tempList = mutableListOf() + for (j in 0..i){ + tempList.add(pascal(i,j)) + } + returnList.add(tempList) + } + return returnList + } + + private fun pascal(i: Int, j: Int): Int { + if (j == 0) { + return 1 + } else if (j == i) { + return 1 + } else { + return pascal(i - 1, j - 1) + pascal(i - 1, j) + } + + } + } +} \ No newline at end of file