• + 1 comment

    Scala recursive version:

        def main(args: Array[String]) {
            val n = StdIn.readInt()
            println("1")
            printPascalTriangle(n, List(1))
        }
        
        @tailrec
        def printPascalTriangle(n: Int, prevRow: List[Int]): Unit = {
            if (prevRow.size < n) {
                val currentRow = 
                    1 :: (for (i <- 1 to prevRow.size) yield 
                            prevRow(i - 1) + 
                                (if (i < prevRow.size) prevRow(i) else 0)
                    ).toList
                println(currentRow.mkString(" "))
                printPascalTriangle(n, currentRow)
            }
        }