Last active
August 29, 2015 14:12
-
-
Save RussellAndrewEdson/181c36fd1928c85abe35 to your computer and use it in GitHub Desktop.
Scala code for Pascal's Triangle.
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 nextRow(row: List[Int]): List[Int] = (row, (0 :: row)).zipped.map(_ + _) | |
/* Generating 5 rows of Pascal's triangle. */ | |
var pascal = List[List[Int]]( List(1,0,0,0,0) ) | |
for (i <- 1 to 4) { | |
pascal = nextRow(pascal.head) :: pascal | |
} | |
pascal = pascal.reverse | |
for (row <- pascal) { | |
for (element <- row) { | |
print(element + " ") | |
} | |
println | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment