• + 1 comment

    I am using Scala. This is the solution I found. It uses local mutation but it works. If you know how to make it more functional, please share:

    def getTable(n: Int = 1001, k: Int = 1001): Array[Array[Long]] = {
      val mutableTable: mutable.ArrayBuffer[Array[Long]] = collection.mutable.ArrayBuffer()
      mutableTable += 1L +: Array.fill(n-1)(0L)
      for (i <- 1 until k) {
        val a1 = mutableTable(i-1) :+ 0L
        val a2 = 0L +: mutableTable(i-1)
        mutableTable += (0 until n).map( e => (a1(e) + a2(e)) % modulo).toArray
      }
      mutableTable.toArray
    }
    
        [...]
    val table = getTable()
        [...]
    println(table(N)(K))