Skip to content

Instantly share code, notes, and snippets.

@moreirayokoyama
Created June 21, 2016 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreirayokoyama/dc29dc1281564ed7d25d5ba0c17899da to your computer and use it in GitHub Desktop.
Save moreirayokoyama/dc29dc1281564ed7d25d5ba0c17899da to your computer and use it in GitHub Desktop.
Pascal Triangle recursive
def PascalTriangleRecursive(row: Int, triangle: List[List[Int]], rows: Int): List[List[Int]] = {
val newRow =
if(row == 0)
List(1)
else {
val lastRow = 0 +: (triangle.last :+ 0)
for((i, j) <- lastRow.init zip lastRow.tail)
yield i + j
}
val newTriangle = triangle :+ newRow
if(row >= rows - 1)
newTriangle
else
loop(row + 1, newTriangle, rows)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment