Last active
March 3, 2018 21:58
-
-
Save mohnoor94/4f3880788dd3bdc5427a1ee266a7e3f1 to your computer and use it in GitHub Desktop.
Functional Programming Principles in Scala - Exercise 1: Pascal’s Triangle
This file contains hidden or 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 Main { | |
def main(args: Array[String]) { | |
println("Pascal's Triangle") | |
for (row <- 0 to 10) { | |
for (col <- 0 to row) | |
print(pascal(col, row) + " ") | |
println() | |
} | |
} | |
/** | |
* Exercise 1 | |
*/ | |
def pascal(c: Int, r: Int): Int = | |
if (c == 0 || c == r) 1 | |
else pascal(c - 1, r - 1) + pascal(c, r - 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This exercise is copied from the Functional Programming Principles in Scala course, week 1. Find more on my website.