Created
June 21, 2016 22:22
-
-
Save moreirayokoyama/dc29dc1281564ed7d25d5ba0c17899da to your computer and use it in GitHub Desktop.
Pascal Triangle recursive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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