You are viewing a single comment's thread. Return to all comments →
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) } }
Seems like cookies are disabled on this browser, please enable them to open this website
Pascal's Triangle
You are viewing a single comment's thread. Return to all comments →
Scala recursive version: