Created
April 5, 2013 14:12
-
-
Save vdupain/5319563 to your computer and use it in GitHub Desktop.
Exercise 1: 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
object pascal { | |
def pascal(c: Int, r: Int): Int = { | |
def loop(c: Int, r: Int): Int = { | |
if (c == r || c == 0) 1 | |
else loop(c, r - 1) + loop(c - 1, r - 1) | |
} | |
loop(c, r) | |
} | |
def pascalTriangle() { | |
println("Pascal's Triangle") | |
for (row <- 0 to 10) { | |
for (col <- 0 to row) | |
print(pascal(col, row) + " ") | |
println() | |
} | |
} | |
pascalTriangle() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment